【问题标题】:How to open .pdf URL in Safari browser in swift [duplicate]如何在 Safari 浏览器中快速打开 .pdf URL [重复]
【发布时间】:2020-06-01 01:11:09
【问题描述】:

在这里,我尝试在 Safari 浏览器 中打开带有 .pdf 扩展名的 URL。但是代码不起作用我在URL 中得到了零。但是当我尝试像 "www.google.com" 之类的 URL 时,它工作正常

  • 代码

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

提前致谢

【问题讨论】:

  • 使用addingPercentEncodingURL进行编码。
  • 您的字符串中有一个空格 "Rahul _"。如果你对它进行百分比编码,它应该可以工作。 .addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)。您还需要将您的域添加到 info plist App Transport Security Settings Exception Domains 列表dropbox.com/s/hi684jdomlrl5r4/…
  • 如果你的服务器支持 https,你应该使用它

标签: swift safari


【解决方案1】:

试试这个。

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
    }
}

【讨论】:

【解决方案2】:

试试这个:

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)

【讨论】:

  • 对不起,您必须先阅读问题,因为您的答案代码与问题代码相同,并且答案代码格式不正确。
  • 此代码在我的应用程序中运行良好,因此我尝试帮助处理此代码...在我的应用程序中:调用 API -> 获取 PDF url -> 打开浏览器以查看 PDF。它在我的应用中运行良好。
  • 我已经尝试过使用代码,但它不起作用,我在字符串到 url 的转换中得到了 nil url。请看问题!
  • @LeoDabus 它在我的任何设备和模拟器中都不起作用!
  • @LeoDabus 更新请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2015-05-22
  • 2015-10-22
  • 2011-09-10
  • 1970-01-01
相关资源
最近更新 更多