2013年7月,WhatsApp爲我們的應用程序打開了他們的URL方案。我從我的應用程序發送文本到WhatsApp,但現在我想發送一個圖像。如何將圖像發送到WhatsApp?如何從我的應用程序發送圖像到WhatsApp?
我不知道它是如何做到的。
謝謝。
2013年7月,WhatsApp爲我們的應用程序打開了他們的URL方案。我從我的應用程序發送文本到WhatsApp,但現在我想發送一個圖像。如何將圖像發送到WhatsApp?如何從我的應用程序發送圖像到WhatsApp?
我不知道它是如何做到的。
謝謝。
根據他們的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]
下面是一個斯威夫特最終工作溶液。該方法由一個按鈕觸發。你可以找到一些解釋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)
}
}
http://www.whatsapp.com/faq/en/iphone/23559013 如果我正確地理解他們,只是讓UIDocumentInteractionController的一個實例,並且圖像初始化它然後應該可以通過UIDocumentInteractionController的內置操作按鈕發送圖像。 – Marc
非常感謝。我嘗試使用UIDocumentInteracionController,並通過WhatsApp發送圖像。 – Paolpa