0

I am running sample app in swift. I need to share image from my app. With the help of Social framework , I integrated Facebook and Twitter. Now I dont know how to integrate with Instagram and WhatsApp

Share Button Action

  @IBAction func shareAcn(sender: UITapGestureRecognizer) {

    var documentController: UIDocumentInteractionController!

    let fileURL = NSBundle.mainBundle().pathForResource("smc", ofType: "png")
    documentController = UIDocumentInteractionController.init(URL: NSURL(fileURLWithPath: fileURL!))
    documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
 }

WhatsApp

 @IBAction func shareAcn(sender: UITapGestureRecognizer) {

   var documentController: UIDocumentInteractionController!
   let image = UIImage(named: "smc")
   let filename = "myimage.wai"
   let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as NSString
   let destinationPath = documentsPath.stringByAppendingString("/" + filename)
   UIImagePNGRepresentation(image!)!.writeToFile(destinationPath, atomically: false)
   let fileURL = NSURL(fileURLWithPath: destinationPath) as NSURL
   documentController = UIDocumentInteractionController.init(URL: fileURL)
   documentController.UTI = "net.whatsapp.image"
   documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
 }

If I select [Import to WhatsApp or Copy to WhatsApp] app getting crashed. If I select WhatsApp, "This Item Cannot Send".

Instagram

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME")
if UIApplication.sharedApplication().canOpenURL(instagramURL) {
    UIApplication.sharedApplication().openURL(instagramURL)
}

None of code is working.

I installed Instagram and logged as my user in my testing device and if I run, Instagram is not opening.

I dont know how to solve this? Kindly guide me.

McDonal_11
  • 3,935
  • 6
  • 24
  • 55

1 Answers1

1

Make sure, you have set the LSApplicationQueriesSchemes entries in the info.plist file, declaring which schemes it attempts to query.

<key>LSApplicationQueriesSchemes</key>  
<array>  
     <string>whatsapp</string>
     <string>instagram</string>  
</array>  

Do not include "://".

If you do include "://" canOpenUrl: will return NO. When you make the actual call of canOpenUrl: you must still include the colon i.e. canOpenUrl:@"instagram:"

You can find more information in this WWDC 2015 video.


Also, this line is suspicious:

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME")

I suspect you did copy this from Instagrams documentation. If so, replace USERNAMEwith your actual username.


To isolate the error, you might want to try a simpler URL first:

var instagramURL: NSURL = NSURL(string: "instagram://app")
shallowThought
  • 19,212
  • 9
  • 65
  • 112