0

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

1 Answers1

0

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
        }
    }
}
emrcftci
  • 3,355
  • 3
  • 21
  • 35
  • @punjabidharti could you please check my answer, my suggestion is first download the asset from URL then use this image to do your stuff. I hope it is work for you. – emrcftci May 29 '20 at 08:51
  • getting error here in url data(from: url) { data, response, error in. Use of unresolved identifier 'url' @emrcftci – punjabidharti May 29 '20 at 08:55
  • this `url` should be your image's URL please check my updated answer – emrcftci May 29 '20 at 08:58