如何使用 Swift 4.2 和 Xcode 11.3 将视频分享到 Instagram Feed?
How do I share a video to instagram feed using Swift 4.2 and Xcode 11.3?
我已经在 iPhone 上安装了 instagram 并且也登录了。
我在 Swift
中使用以下代码
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
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 alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
在URL类型的项目目标设置中,我在URL方案中添加了“instagram://library”。这是错误的吗?这里应该添加什么?因为 UIApplication.shared.canOpenURL 总是 returns false.
将 info.plist
文件中的 URLScheme
修改为正确的方案,最后不要包含任何其他内容。修改instagram://library
为instagram
.
我已经在 iPhone 上安装了 instagram 并且也登录了。 我在 Swift
中使用以下代码let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
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 alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
在URL类型的项目目标设置中,我在URL方案中添加了“instagram://library”。这是错误的吗?这里应该添加什么?因为 UIApplication.shared.canOpenURL 总是 returns false.
将 info.plist
文件中的 URLScheme
修改为正确的方案,最后不要包含任何其他内容。修改instagram://library
为instagram
.