Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

swift - Add Instagram to UIActivityViewController

I'm trying to share an image using standard UIActivityViewController, it's fine to share on Facebook, Twitter and Save Image using this code:

let firstActivityItem = "foo text"
let secondActivityItem : UIImage = image!

let activityViewController : UIActivityViewController = UIActivityViewController(
    activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

activityViewController.excludedActivityTypes = [
    UIActivityTypePostToWeibo,
    UIActivityTypePrint,
    UIActivityTypeAssignToContact,
    UIActivityTypeAddToReadingList,
    UIActivityTypePostToVimeo,
    UIActivityTypePostToTencentWeibo
]

self.presentViewController(activityViewController, animated: true, completion: nil)

I need one more thing, Instagram:

If UIApplication.sharedApplication().canOpenURL(instagramURL!) {
            // Success
            var img = image!

            var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.igo")
            UIImageJPEGRepresentation(img, 1).writeToFile(savePath, atomically: true)
            var imgURL = NSURL(string: NSString(format: "file://%@", savePath) as! String)
            docController = UIDocumentInteractionController(URL: imgURL!) // 1
            docController.UTI = "com.instagram.exclusivegram" // 2
            docController.delegate = self
            docController.annotation = ["InstagramCaption":"testsss"] // 3
            docController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) // 4
        } else {
            // Error
        }    

Both these codes work fine separately, how can I add Instagram to the UIActivityViewController? Is it possible at all?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...