是否可以绕过行动共享表将照片分享给Instagram?
请注意,我知道UIDocumentInteractionController
和钩子,事实上它工作正常。使用他们的示例代码,您将获得Copy to Instagram
选项(如果您使用独家UTI或可以处理JPG / PNG的大型应用程序列表,以及Instagram,则可以选择)。
这很好,但我想知道是否有办法执行“复制到Instagram”操作,而无需在iOS 9+中显示UIDocumentInteractionController
菜单。
为了记录,这是完美运行的代码的简化版本。假设你有一个有效的NSURL ......
guard let data: NSData = NSData(contentsOfURL: url),
image = UIImage(data: data) else {
return
}
let imageData = UIImageJPEGRepresentation(image, 100)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
guard let _ = imageData?.writeToFile(writePath, atomically: true) else {
return
}
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
self.documentController.presentOpenInMenuFromRect(viewController.view.frame, inView: viewController.view, animated: true)
问题是这将提供一个“操作表”,我想避免这样做,如果可能的话,我想使用instagram.ige
(或任何名称使其独占)并跳过此ActionSheet。
那可能吗?
更新:我还没有找到解决方案,但似乎Instagram最终添加/添加了扩展:“Instagram最近在其iOS应用程序中添加了共享扩展功能。现在,您可以将第三方应用程序的照片直接分享到Instagram”来源:http://www.macworld.com/article/3080038/data-center-cloud/new-instagram-feature-for-ios-makes-it-easier-to-share-photos-from-other-apps.html
更新的Swift 4.2代码
import Photos
func postImageToInstagram(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(
image,
self,
#selector(self.image(image:didFinishSavingWithError:contextInfo:)),
nil
)
}
@objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let err = error {
print(err) // TODO: handle error
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 u = "instagram://library?AssetPath=" + localIdentifier
let url = URL(string: u)!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, 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)
}
}
}
原始答案
import Photos
...
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
let url = NSURL(string: u)!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(NSURL(string: u)!)
} else {
let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}