如何从我的应用程序发送图像到WhatsApp?

2013年7月,WhatsApp为我们的应用程序打开了他们的URLscheme。 我已经从我的应用程序发送文本到WhatsApp,但现在我想发送一个图像。 如何将图像发送到WhatsApp?

我不知道该怎么做。

谢谢。

根据他们的文档 ,你需要使用UIDocumentInteractionController 。 要有select地只显示文档控制器中的Whatsapp(它提供给用户,在这一点上,他们可以selectWhatsApp分享),按照他们的指示:

或者,如果您只想在应用程序列表中显示WhatsApp(而不是WhatsApp加上任何其他公共/ *符合应用程序),则可以指定保存在WhatsApp专有的扩展名中的上述types的文件:

 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 

您需要将图像保存到磁盘,然后使用该文件URL创build一个UIDocumentInteractionController

以下是一些示例代码:

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

这是Swift最后的工作解决scheme。 该方法由一个button触发。 你可以在这里find更多的解释

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