如何与Instagram应用共享UIImage?
我知道,这个话题已经讨论了很多次。但是由于SWIFT的发展,我希望找到一个更好的解决方案。我发现了两种不同的实现方法,两种方法都可以在我的设备上运行:
1。)第一种方法:UIApplication.shared.open()
func backgroundImage(_ backgroundImage: Data, attributionURL: String) {
// Verify app can open custom URL scheme, open if able
guard let urlScheme = URL(string: "instagram-stories://share"),
UIApplication.shared.canOpenURL(urlScheme) else {
// Handle older app versions or app not installed case
return
}
let pasteboardItems = [["com.instagram.sharedSticker.backgroundImage": backgroundImage,
"com.instagram.sharedSticker.contentURL": attributionURL]]
let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]
UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)
UIApplication.shared.open(urlScheme)
}
这将直接在Instagram应用中打开我的图片。到目前为止还不错,但是通过这种方式,只能在 Instagram故事中共享。
2。)第二种方法:UIDocumentInteractionController()
private let kInstagramURL = "instagram://"
private let kUTI = "com.instagram.exclusivegram"
private let kfileNameExtension = "instagram.igo"
[...]
if UIApplication.shared.canOpenURL(instagramURL! as URL) {
let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(kfileNameExtension)
do {
try UIImageJPEGRepresentation(imageInstagram, 0.75)?.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)
} catch {
print(error)
}
let rect = CGRect.zero
let fileURL = NSURL.fileURL(withPath: jpgPath)
documentInteractionController.url = fileURL
documentInteractionController.delegate = self
documentInteractionController.uti = kUTI
// adding caption for the image
documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
//documentInteractionController.presentOptionsMenu(from: rect, in: view, animated: true)
}
这将打开UIDocumentInteractionController弹出窗口,用户可以在其中选择要共享的应用程序。不幸的是,许多应用程序都可以处理“ instagram.igo”: Screenshot of Popup
我现在的问题:
我的问题: 有一种解决方案可以直接打开应用程序,而不会弹出窗口!但是是否有可能直接到达Instagram选择屏幕而没有弹出窗口? Instagram Selection Screen
中没有帮助