How send image to WhatsApp from my application?

▼魔方 西西 提交于 2019-11-30 19:26:17

问题


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 Whatsapp?

I'm not sure how do it.

Thank you.


回答1:


Per their documentation, you need to use UIDocumentInteractionController. To selectively display only Whatsapp in the document controller (it is presented to the user, at which point they can select Whatsapp to share to), follow their instructions:

Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:

images - «.wai» which is of type net.whatsapp.image
videos - «.wam» which is of type net.whatsapp.movie
audio files - «.waa» which is of type net.whatsapp.audio

You need to save the image to disk, and then create a UIDocumentInteractionController with that file URL.

Here is some example code:

_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL];
_documentController.delegate = self;
_documentController.UTI = @"net.whatsapp.image";
[_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]



回答2:


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)
    }
}


来源:https://stackoverflow.com/questions/19524635/how-send-image-to-whatsapp-from-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!