[英]How send image to WhatsApp from my application?
2013年7月,WhatsApp为我们的应用程序开放了他们的URL方案。 我已经从我的应用程序发送文本到Whatsapp,但现在我想发送一个图像。 如何将图像发送到Whatsapp?
我不知道怎么做。
谢谢。
根据他们的文档 ,您需要使用UIDocumentInteractionController
。 要在文档控制器中有选择地仅显示Whatsapp(它呈现给用户,此时他们可以选择要分享的Whatsapp),请按照他们的说明操作:
或者,如果您只想在应用程序列表中显示WhatsApp(而不是WhatsApp以及任何其他符合公共/ *的应用程序),您可以指定使用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
您需要将映像保存到磁盘,然后使用该文件URL创建UIDocumentInteractionController
。
这是一些示例代码:
_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL];
_documentController.delegate = self;
_documentController.UTI = @"net.whatsapp.image";
[_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]
这是Swift的最终解决方案。 该方法由按钮触发。 你可以在这里找到更多解释
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.