如何将Instagram活动添加到UIActivityViewController

时间:2017-02-07 14:29:39

标签: ios swift instagram uiactivityviewcontroller

我无法找到向UIActivityViewController添加Instagram选项的解决方案。有谁可以帮忙。感谢。

Instagram activity in UIActivityViewController

1 个答案:

答案 0 :(得分:-1)

使用类似的东西:

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

要使此代码有效,请不要忘记在UIViewController类中添加UIDocumentInteractionControllerDelegate