我正在我的应用程序中捕获图像,并使用WHATSAPP通过按下分享按钮来分享它,就像在RETRICA中一样。但我找不到任何方法来正确地完成它。我使用了UIDocumentInteraction,但它不起作用。如何在IOS8中使用WHATSAPP的share扩展来共享它。
我在使用UIDocumentInteractionController时遇到了这个异常。
'UIDocumentInteractionController:方案无效(null)。仅支持文件方案。‘
这是我的代码
let image = UIImage(named: "nature")
let path = NSHomeDirectory().stringByAppendingPathComponent("Documents/whatsAppTmp.wai")
UIImageJPEGRepresentation(image!, 100.0)?.writeToFile(path, atomically: true)
let documentInteractionController = UIDocumentInteractionController(URL: NSURL(string: path)!)
documentInteractionController.UTI = "net.whatsapp.image"
发布于 2016-03-22 02:33:25
也许这对你有帮助:
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
if let image = UIImage(named: "image") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.writeToURL(tempFile, options: .DataWritingAtomic)
self.documentInteractionController = UIDocumentInteractionController(URL: tempFile)
self.documentInteractionController.UTI = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
发布于 2016-10-28 04:29:41
在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
https://stackoverflow.com/questions/32463224
复制相似问题