简体   繁体   中英

Share Image On Whatsapp Via URL

I am creating Images app from my WordPress website with json and i am using swift, i want to share image on whatsapp from my app, currently i tried this code it works but only with image name i want to share image from image url, is that possible?

this is my code

 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: "splash") {
                        if let imageData = image.jpegData(compressionQuality: 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
                  }
              }
          }

Thanks

Firstly you should download an image from URL

  • Create a function to get data
func data(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}

What to do for this ->

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) {

            // Set your image's URL into here
            let url = URL(string: "https://your-image-url.com")!
            data(from: url) { data, response, error in
                guard let data = data, error == nil else { return }
                DispatchQueue.main.async() { [weak self] in

                    let image = UIImage(data: data)
                    if let imageData = image.jpegData(compressionQuality: 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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM