2 Replies
      Latest reply on Oct 26, 2016 4:05 AM by eskimo
      EricMacau Level 1 Level 1 (0 points)



        I tried some example that provides in the web for sharing an image to Whatsapp.


        The same codes of function in Objective-C works fine. It can show the action sheet for selection of the WhatsApp and I can select one of the contact to share.

        The same codes in Swift, it can show the action sheet for selection of the WhatsApp but after selecting the WhatsApp icon, it did nothing and it cannot show me the WhatsApp apps to be for selecting the contact.


        The following is my Swift code:

        import UIKit
        class ViewController: UIViewController {
            var controller: UIDocumentInteractionController = UIDocumentInteractionController()
            override func viewDidLoad() {
            override func didReceiveMemoryWarning() {
            @IBAction func doShare(sender: AnyObject) {
                let iconImage = UIImage(named: "icon")
                let savePath  = NSHomeDirectory().stringByAppendingString("Documents/whatsAppTmp.wai")
                UIImageJPEGRepresentation(iconImage!, 1.0)?.writeToFile(savePath, atomically: true)
                let imageURL = NSURL.fileURLWithPath(savePath)
                print("Image path :\(savePath)")
                controller.UTI = "net.whatsapp.image"
                controller.URL = imageURL
                controller.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)


        I tested it in iOS 9.


        Any expert can teach if there is some problem or something I missed?


        Best reards,

        • Re: Share image to Whatsapp not working in Swift
          sanaulx Level 1 Level 1 (0 points)

          change this line:

          let savePath  = NSHomeDirectory().stringByAppendingString("Documents/whatsAppTmp.wai")




          let savePath  = NSHomeDirectory().stringByAppendingString("/Documents/whatsAppTmp.wai")


          You missed back-slash ("/") on savePath

            • Re: Share image to Whatsapp not working in Swift
              eskimo Apple Staff Apple Staff (13,925 points)

              You missed back-slash ("/") on savePath

              Just FYI, it’s generally better to avoid dealing with path separators wherever possible.  The best way to do this is to stay in ‘URL space’.  For example:

              • get the Documents directory using FileManager’s url(for:in:appropriateFor:create:)

              • going ‘down’ the file system hierarchy with appendingPathComponent(_:) or appendingPathComponent(_:isDirectory:)

              • going ‘up’ the hierarchy using deletingLastPathComponent()

              Share and Enjoy

              Quinn “The Eskimo!”
              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
              let myEmail = "eskimo" + "1" + "@apple.com"