swift - Add Instagram to UIActivityViewController -
i'm trying share image using standard uiactivityviewcontroller
, it's fine share on facebook, twitter , save image using 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 1 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 add instagram uiactivityviewcontroller
? possible @ all?
i think easier add other social shares code wrote instagram. ".igo" extension exclusive instagram other apps not support it. change extension ".igo" ".ig" , other apps read it:
var savepath: string = nshomedirectory().stringbyappendingpathcomponent("documents/test.ig")
but instagram have exclusive uti avoiding other apps appear in same document interaction view. need change "exclusivegram" "photo":
doccontroller.uti = "com.instagram.photo"
i have app similar functionality , 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() 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 not installed ") } }
to make code work, don't forget add uidocumentinteractioncontrollerdelegate
in uiviewcontroller class.
Comments
Post a Comment