【问题标题】:Share image via whatsapp ios 8通过whatsapp ios 8分享图片
【发布时间】:2016-10-28 13:21:46
【问题描述】:

我正在我的应用程序中捕获图像,并通过按 RETRICA 中的共享按钮使用 WHATSAPP 共享它。但我没有找到任何方法来正确地做到这一点。我使用了 UIDocumentInteraction 但它没有用。我如何在 IOS8 中使用 WHATSAPP 的共享扩展来共享它。

我在使用 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"

【问题讨论】:

  • 你试过使用 UIActivityViewController 吗?

标签: ios swift whatsapp


【解决方案1】:

也许这可以帮助你:

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
        }
    }
}

你可以看到这个答案 Share image/text through WhatsApp in an iOS app

【讨论】:

    【解决方案2】:

    在 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

    【讨论】:

    • 不工作。向 whatsapp 发送文件,但无法以图片形式打开。
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多