1

I am capturing an image in my app and what to share it using WHATSAPP by pressing share button like in RETRICA. But i didn't find any way to do it properly. i used UIDocumentInteraction but it didn't work. How can i share it using share extension of WHATSAPP in IOS8.

I got this exception while using UIDocumentInteractionController.

'UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported.'

this is my code

let image = UIImage(named: "nature")
        let path = NSHomeDirectory().stringByAppendingPathComponent("Documents/whatsAppTmp.wai")
        UIImageJPEGRepresentation(image!, 100.0)?.writeToFile(path, atomically: true)

        let documentInteractionController = UIDocumentInteractionController(URL: NSURL(string: path)!)
        documentInteractionController.UTI = "net.whatsapp.image"
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Wahib
  • 85
  • 1
  • 11

2 Answers2

0

Maybe this can help you:

let urlWhats = "whatsapp://app"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
    if let whatsappURL = NSURL(string: urlString) {

        if UIApplication.sharedApplication().canOpenURL(whatsappURL) {

            if let image = UIImage(named: "image") {
                if let imageData = UIImageJPEGRepresentation(image, 1.0) {
                    let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai")
                    do {
                        try imageData.writeToURL(tempFile, options: .DataWritingAtomic)
                        self.documentInteractionController = UIDocumentInteractionController(URL: tempFile)
                        self.documentInteractionController.UTI = "net.whatsapp.image"
                        self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
                    } catch {
                        print(error)
                    }
                }
            }

        } else {
            // Cannot open whatsapp
        }
    }
}

You can see this answer Share image/text through WhatsApp in an iOS app

Community
  • 1
  • 1
Wagner Sales
  • 1,448
  • 2
  • 10
  • 14
0

In Swift 3 use this code

 @IBAction func whatsappShareWithImages(_ sender: AnyObject)
    {

        let urlWhats = "whatsapp://app"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
            if let whatsappURL = URL(string: urlString) {

                if UIApplication.shared.canOpenURL(whatsappURL as URL) {

                    if let image = UIImage(named: "whatsappIcon") {
                        if let imageData = UIImageJPEGRepresentation(image, 1.0) {
                            let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
                            do {
                                try imageData.write(to: tempFile, options: .atomic)
                                self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
                                self.documentInteractionController.uti = "net.whatsapp.image"
                                self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

                            } catch {
                                print(error)
                            }
                        }
                    }

                } else {
                    // Cannot open whatsapp
                }
            }
        }

    }

Add this code in your app "plist"

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

You can also refer to small app for reference : https://github.com/nithinbemitk/iOS-Whatsapp-Share

Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27