首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过whatsapp ios 8共享映像

通过whatsapp ios 8共享映像
EN

Stack Overflow用户
提问于 2015-09-09 00:53:00
回答 2查看 1.7K关注 0票数 1

我正在我的应用程序中捕获图像,并使用WHATSAPP通过按下分享按钮来分享它,就像在RETRICA中一样。但我找不到任何方法来正确地完成它。我使用了UIDocumentInteraction,但它不起作用。如何在IOS8中使用WHATSAPP的share扩展来共享它。

我在使用UIDocumentInteractionController时遇到了这个异常。

'UIDocumentInteractionController:方案无效(null)。仅支持文件方案。‘

这是我的代码

代码语言:javascript
复制
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"
EN

回答 2

Stack Overflow用户

发布于 2016-03-22 10:33:25

也许这对你有帮助:

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2016-10-28 12:29:41

在Swift 3中使用以下代码

代码语言:javascript
复制
 @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“中

代码语言:javascript
复制
   <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>whatsapp</string>
        </array>

你也可以参考一下小应用:https://github.com/nithinbemitk/iOS-Whatsapp-Share

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32463224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档