2

Here i trying to open URL with .pdf extension in Safari Browser. But code is not working I'm getting nil in URL. But when i tried URL like "www.google.com" it's working fine

  • Code

       let cerificateURl = "https://file-examples.com/wp-content/uploads/2017/10/file-sample_150kB.pdf"
    
        guard let url = URL(string: cerificateURl) else {
          return
        }
    
       if #available(iOS 10.0, *) {
           UIApplication.shared.open(url, options: [:], completionHandler: nil)
       } else {
          UIApplication.shared.openURL(url)
       }
    

Thanks in Advance

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
  • Encode that `URL` by using `addingPercentEncoding`. – Piyush Feb 17 '20 at 06:54
  • There is a whitespace **"Rahul _"** in your string. If you percent encode it it should work. `.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)`. You will also need to add your domain to the info plist App Transport Security Settings Exception Domains list https://www.dropbox.com/s/hi684jdomlrl5r4/app%20transport%20security.jpg?dl=1 – Leo Dabus Feb 17 '20 at 07:30
  • If your server supports https you should use it – Leo Dabus Feb 17 '20 at 07:35

2 Answers2

-1

Try this one:

guard let url = URL(string: "your web pdf file link here or you can set from local directory path") 
else { return }
UIApplication.shared.open(url)
Hassan ElDesouky
  • 334
  • 4
  • 17
Khushal iOS
  • 303
  • 2
  • 12
-2

Try This one.

let cerificateURl = "https://file-examples.com/wp-content/uploads/2017/10/file-sample_150kB.pdf"

if let myURL = URL(string: url) {
    if let data = try? Data(contentsOf: myURL) {
        webView.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL
    }
}
Rushabh Shah
  • 396
  • 3
  • 19
  • Sorry but I've to open pdf url in browser not in webView! – steveSarsawa Feb 17 '20 at 06:20
  • 3
    You should use URLSession dataTask to download your data asynchronously. This would freeze your app if there is no internet connection. https://developer.apple.com/documentation/foundation/urlsession/1407613-datatask – Leo Dabus Feb 17 '20 at 06:20