2013年7月,WhatsApp为我们的应用程序开放了他们的URL方案。我已经从我的应用程序发送文本到Whatsapp,但现在我想发送一个图像。如何将图像发送给Whatsapp?
我不知道怎么做。
谢谢。
答案 0 :(得分:6)
根据documentation,您需要使用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]
答案 1 :(得分:2)
这是Swift的最终解决方案。该方法由按钮触发。你可以找到更多解释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)
}
}