Add Instagram to UIActivityViewController

后端 未结 2 1151
孤独总比滥情好
孤独总比滥情好 2020-12-15 14:33

I\'m trying to share an image using standard UIActivityViewController, it\'s fine to share on Facebook, Twitter and Sa

相关标签:
2条回答
  • 2020-12-15 15:33

    I think it would be very easier to add other social shares to the code you wrote for Instagram. The ".igo" extension is exclusive for Instagram so other apps will not support it. Just change this extension from ".igo" to ".ig" and other apps will read it:

    var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.ig")
    

    But Instagram also have an exclusive UTI to avoiding other apps to appear in the same Document Interaction View. So you will also need to change it from "exclusivegram" to "photo":

    docController.UTI = "com.instagram.photo"
    

    I have an app with a similar functionality and this is my original code:

    @IBAction func shareOnIntagram(sender: UIButton) {
    
            let finalImage: UIImage = UIImage.imageWithView(photoView)
            let instagramURL = NSURL(string: "instagram://app")
            if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
    
                let imageData = UIImageJPEGRepresentation(finalImage, 1)
                let captionString = "caption"
                let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
                if imageData?.writeToFile(writePath, atomically: true) == false {
    
                    return
    
                } else {
    
                    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(self.view.frame, inView: self.view, animated: true)
                }
    
            } else {
                print(" Instagram is not installed ")
            }
        }
    

    To make this code work, don't forget to add UIDocumentInteractionControllerDelegate in the UIViewController class.

    0 讨论(0)
  • 2020-12-15 15:35

    It seems it's not possible, because of .igo extension which is needed by Instagram.

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