How send image to WhatsApp from my application?

前端 未结 2 753
故里飘歌
故里飘歌 2021-01-07 02:55

In July 2013 WhatsApp opened their URL schemes for our apps. I have sent text to Whatsapp from my application, but now I would like send a image. How send a image to Whatsap

2条回答
  •  一个人的身影
    2021-01-07 03:36

    Here is a finally working solution for Swift. The method is triggered by a button. you can find some more explanations here

    import UIKit
    
    class ShareToWhatsappViewController: UIViewController, UIDocumentInteractionControllerDelegate {
        var documentController: UIDocumentInteractionController!
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        @IBAction func shareToWhatsapp(sender: UIButton) {
            let image = UIImage(named: "my_image") // replace that with your UIImage
    
            let filename = "myimage.wai"
            let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString
            let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath
            UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false)
            let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL
    
            documentController = UIDocumentInteractionController(URL: fileUrl)
            documentController.delegate = self
            documentController.UTI = "net.whatsapp.image"
            documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false)
        }
    }
    

提交回复
热议问题