【发布时间】:2012-12-21 01:24:18
【问题描述】:
是否可以在 iPhone 应用程序开发中使用 UIDocumentIntractionController 或 API 将 iOS 应用程序中的图像和文本共享到 WhatsApp?
【问题讨论】:
标签: ios image sharing whatsapp
是否可以在 iPhone 应用程序开发中使用 UIDocumentIntractionController 或 API 将 iOS 应用程序中的图像和文本共享到 WhatsApp?
【问题讨论】:
标签: ios image sharing whatsapp
请查看常见问题解答中的此链接:
【讨论】:
我发现如果您的应用程序创建了照片、视频或音频笔记,并且您希望您的用户使用 WhatsApp 共享这些媒体,您可以使用文档交互 API 将您的媒体发送给您的 WhatsApp 联系人和群组。
【讨论】:
在 Swift 3 中使用此代码
@IBAction func whatsappShareWithImages(_ sender: AnyObject)
{
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
if let image = UIImage(named: "whatsappIcon") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
}
将此代码添加到您的应用“plist”中
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
您也可以参考小应用:https://github.com/nithinbemitk/iOS-Whatsapp-Share
【讨论】: