2015-09-08 69 views
1

我在我的应用程序中捕获图像,以及如何使用WHATSAPP通过按共享按钮(如RETRICA中)来分享图像。但我没有找到任何正确的方法。我用UIDocumentInteraction但它没有工作。我如何在IOS8中使用WHATSAPP的共享扩展来共享它。通过whatsapp分享图像ios 8

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

'UIDocumentInteractionController:invalid scheme(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" 
+1

您是否尝试过使用UIActivityViewController? – MXV

回答

0

也许这可以帮助你:

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

在斯威夫特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

+0

不工作。发送文件到whatsapp,但不能作为Image打开。 –