【问题标题】:iOS - Send Image to Instagram - DocumentInteractioniOS - 将图像发送到 Instagram - DocumentInteraction
【发布时间】:2016-03-31 23:58:34
【问题描述】:

是否可以绕过 Action Share Sheet 将图片分享到 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(或任何使其独占的名称) ) 并跳过此操作表。

这可能吗?

更新:我还没有找到解决方案,但似乎 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

【问题讨论】:

  • 您找到解决方案了吗?似乎即使在 Instagram 进行了最新更新之后,您仍然需要在打开 Instagram 而不是直接打开 Instagram 之前使用共享表。
  • 我没有,但老实说我也没有再试一次。我相信(如果我没记错的话)客户并不关心它并决定单独处理 Instagram。跛脚。

标签: ios swift instagram uiactionsheet uidocumentinteraction


【解决方案1】:

更新的 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)
            }

        }
}

【讨论】:

  • 要完成这项工作,需要在 plist 中添加以下内容: LSApplicationQueriesSchemesinstagram
  • 有没有办法在不保存到相册的情况下做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-25
  • 1970-01-01
相关资源
最近更新 更多