Share image on whatsapp using Swift

后端 未结 3 1850
深忆病人
深忆病人 2021-01-15 19:03

I\'m creating an application to share images via social media platforms but on WhatsApp especially. I tried using UIActivityViewController but it doesn\'t displ

3条回答
  •  猫巷女王i
    2021-01-15 20:04

    In Swift 3 use this code

    @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 {
                   print("Cannot open whatsapp")
                }
            }
        }
    
    }
    

    Add this code in your app **plist**

    LSApplicationQueriesSchemes
    
        whatsapp
    
    

    You can also refer to small app for reference : https://github.com/nithinbemitk/iOS-Whatsapp-Share

提交回复
热议问题