在iOS 12.4之前的版本中,将照片共享到Instagram feed的实现(在documentation之后)正常工作,但是自iOS 13起,它不再起作用。
[使用当前实现-UIDocumentInteractionController
的UTI设置为"com.instagram.exclusivegram"
,文件扩展名设置为.igo
-根本没有可见的Instagram共享选项。当我将文件扩展名更改为.ig
时,可以在建议中看到Instagram共享选项。这种共享供稿的方式有效,但这不是预期的仅Instagram解决方案。
将UTI设置为"com.instagram.photo"
不会进行任何更改。
预期的行为是,当我按下“共享”按钮时,可以看到下面的视图,无需执行其他步骤。这可能是Instagram的错误,还是有其他实现iOS 13的方法?
您应该将您的图像添加到照片库,然后将其直接从图像共享到instagram
首先不要忘记将NSPhotoLibraryAddUsageDescription和instagram方案添加到您的info.plist:
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) wants to save pictures to your library</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
对于12.4和13 iOS正确工作
import UIKit
import Photos
class TestViewController: UIViewController, UIDocumentInteractionControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
postImageToInstagram(UIImage(named: "bigImage")!)
}
func postImageToInstagram(_ image: UIImage) {
// Check if we have instagarm app
if UIApplication.shared.canOpenURL(URL(string: "instagram://app")!) {
// Requesting authorization to photo library in order to save image there
PHPhotoLibrary.requestAuthorization { status in
if status == .authorized {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
} else { print("wrong status \(status)") }
}
} else { print("Please install the Instagram application") }
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
print(error)
return
}
let fetchOptions = PHFetchOptions()
// add sorting to take correct element from fetchResult
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.fetchLimit = 1
// taking our image local Identifier in photo library to share it
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
let url = URL(string: "instagram://library?LocalIdentifier=\(lastAsset.localIdentifier)")!
if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) }
else { print("Please install the Instagram application") }
}
}
}