iOS - Send Image to Instagram - DocumentInteraction

后端 未结 1 1976
温柔的废话
温柔的废话 2021-02-14 20:48

Would it be possible to share a picture to Instagram bypassing the Action Share Sheet?

Please Note that I am aware of the UIDocumentInte

1条回答
  •  时光说笑
    2021-02-14 21:52

    UPDATED Swift 4.2 CODE

    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)
                }
            }
        }
    

    Original Answer

    import Photos
    ...
    
    func postImageToInstagram(image: UIImage) {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
    }
    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer) {
            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)
                }
    
            }
    }
    

    0 讨论(0)
提交回复
热议问题