[英]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.