【发布时间】:2019-07-29 00:41:51
【问题描述】:
现在,我正在开发 iOS、Android 应用程序。我希望添加一个功能,直接将我的应用程序中的图像轻松分享到 Instagram Story。例如,点击我的 App 上的按钮,然后切换到 Instagram Story,用户可以在 Story 上分享。
我搜索了一些API,没有找到好的方法。请告诉我是否可以开发这样的功能。
【问题讨论】:
标签: swift react-native
现在,我正在开发 iOS、Android 应用程序。我希望添加一个功能,直接将我的应用程序中的图像轻松分享到 Instagram Story。例如,点击我的 App 上的按钮,然后切换到 Instagram Story,用户可以在 Story 上分享。
我搜索了一些API,没有找到好的方法。请告诉我是否可以开发这样的功能。
【问题讨论】:
标签: swift react-native
@IBAction func shareOnInstagram(_ sender: Any) {
DispatchQueue.main.async {
//Share To Instagram:
let instagramURL = URL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL!) {
let imageData = UIImageJPEGRepresentation(image, 100)
let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")
do {
try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)
} catch {
print(error)
}
let fileURL = URL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(url: fileURL)
self.documentController.delegate = self
self.documentController.uti = "com.instagram.exlusivegram"
if UIDevice.current.userInterfaceIdiom == .phone {
self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
} else {
self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true)
}
} else {
print(" Instagram is not installed ")
}
}
}
您可以使用上面的代码分享到Instagram,您需要在项目的LSApplicationQueriesSchemes中设置instagraminfo.plist
【讨论】:
try this:-
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) //.image to share image and .video to share video
if let lastAsset = fetchResult.firstObject {
let localIdentifier = lastAsset.localIdentifier
let u = "instagram://library?LocalIdentifier=" + localIdentifier
let url = NSURL(string: u)!
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.open(URL(string: u)!, options: [:], completionHandler: nil)
} else {
let urlStr = "https://itunes.apple.com/in/app/instagram/id389801252?mt=8"
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(URL(string: urlStr)!)
}
}
}
【讨论】: