是否可以绕过行动共享表将照片分享给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
解决方法
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) } } }