Ios 在UIDocumentInteractionController或UIActivityViewController中自动选择Instagram

Ios 在UIDocumentInteractionController或UIActivityViewController中自动选择Instagram,ios,objective-c,instagram,uiactivityviewcontroller,uidocumentinteraction,Ios,Objective C,Instagram,Uiactivityviewcontroller,Uidocumentinteraction,UIDocumentInteractionController和UIActivityViewController都提供用于向其他网络共享图像的菜单。点击Instagram后,你会看到一个很好的模式,允许你在不离开应用程序的情况下发布到Instagram。我的问题是,如何在不显示菜单的情况下自动显示该模式 这个名为Sounds的应用程序做到了这一点,所以我知道这是可能的,但我在网上找不到任何关于它是如何实现的文档。以下是显示菜单的当前代码: NSString *documentDirect

UIDocumentInteractionController和UIActivityViewController都提供用于向其他网络共享图像的菜单。点击Instagram后,你会看到一个很好的模式,允许你在不离开应用程序的情况下发布到Instagram。我的问题是,如何在不显示菜单的情况下自动显示该模式

这个名为Sounds的应用程序做到了这一点,所以我知道这是可能的,但我在网上找不到任何关于它是如何实现的文档。以下是显示菜单的当前代码:

    NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.ig"];
    NSData *imageData=UIImagePNGRepresentation(cardImage);
    [imageData writeToFile:saveImagePath atomically:YES];
    NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];

    UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
    docController.UTI = @"com.instagram.exclusivegram";
    docController = [self setupControllerWithURL:imageURL usingDelegate:self];
    [docController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];
这是菜单:

以下是我想自动显示的内容:


要删除显示的其他应用程序,请更改以下代码行(只需翻转它们):

因此,在重写docController对象后设置.UTI变量


编辑:如果您试图直接打开Instagram应用程序并填写信息,然后准备好共享,请查看iPhone挂钩。

我用于在Instagram上共享图像的代码

import UIKit
import Photos

class SocialShare: NSObject {
    static let shared = SocialShare()

    func postImageToInstagram(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
    }
    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject as? PHAsset {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?LocalIdentifier=" + localIdentifier
            UIApplication.sharedApplication().openURL(NSURL(string: u)!)
        }
    }
}

只要活动项中只有UIImage,您就可以使用UIActivityViewController显示Instagram。 如果您添加更多的项目,如文本、url等,它将不会显示。 这是Instagram的一个很糟糕的限制

像这样:

NSMutableArray *items = [NSMutableArray array];
[items addObject:[UIImage ...]];

// show view
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
                                                                             applicationActivities:nil];
[vc presentViewController:activityVC animated:YES completion:nil];

下面是一个Swift 4类,它实现了Zuhair的答案

import Foundation
import Photos

class InstagramSharer: NSObject {
    static let shared = InstagramSharer()

    func post(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
    }

    @objc
    private func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        guard error == nil else {
            return
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

        if let lastAsset = fetchResult.firstObject {
            let localIdentifier = lastAsset.localIdentifier
            let url = "instagram://library?LocalIdentifier=" + localIdentifier
            UIApplication.shared.openURL(URL(string: url)!)
        }
    }
}
用法


我知道这是一个非常具体的要求,但iPhone挂钩并不是我们真正想要的。我们不想离开应用程序,我们只想自动启动该模式。这个应用程序听起来是()做的。单击Instagram按钮,它会立即显示该模式。你知道这是怎么做到的吗?这正是我在谷歌上搜索了两天的结果。你找到答案了吗?我见过应用程序这样做,所以我知道这是可能的。你找到解决问题的方法了吗?我也有同样的问题。
NSMutableArray *items = [NSMutableArray array];
[items addObject:[UIImage ...]];

// show view
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
                                                                             applicationActivities:nil];
[vc presentViewController:activityVC animated:YES completion:nil];
import Foundation
import Photos

class InstagramSharer: NSObject {
    static let shared = InstagramSharer()

    func post(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
    }

    @objc
    private func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        guard error == nil else {
            return
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

        if let lastAsset = fetchResult.firstObject {
            let localIdentifier = lastAsset.localIdentifier
            let url = "instagram://library?LocalIdentifier=" + localIdentifier
            UIApplication.shared.openURL(URL(string: url)!)
        }
    }
}
InstagramSharer.shared.post(image: #YOUR_IMAGE#)