Swift:从文件系统显示PDF时符合ATS政策

时间:2018-09-09 06:26:55

标签: swift

我正在尝试从HTML文件生成pdf,然后在webView上显示该生成的pdf。但是,我的实现将一致性问题扔给了ATS策略,这确实使我感到困惑。我的实现是这样的:

func export() {
    let filePath = Bundle.main.path(forResource: "index", ofType: "html")

    do {
        let htmlContent = try String(contentsOfFile: filePath!)

        if let pdfFileName = exportHTMLContentToPDF(HTMLContent: htmlContent) {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let url = URL(fileURLWithPath: documentsPath, isDirectory: true).appendingPathComponent(pdfFileName).appendingPathExtension("pdf")

            let vc = WebViewController()
            vc.url = url
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
        }

    } catch let error {
        print(error.localizedDescription)
    }

}

func exportHTMLContentToPDF(HTMLContent: String) -> String? {
    let printPageRenderer = CustomPrintPageRenderer()

    let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
    printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)

    if let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer) {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let pdfFilename = "\(documentsPath)/Exports.pdf"
        pdfData.write(toFile: pdfFilename, atomically: true)
        print(pdfFilename)
        return pdfFilename
    }

    return nil
}

//At WebViewController's viewDidLoad
var url: URL?
override func viewDidLoad() {
    super.viewDidLoad()

    if let url = url {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

我在这里抛出的错误消息:

  

App Transport Security已阻止明文HTTP(http://)资源   加载,因为它不安全。临时异常可以通过配置   您应用的Info.plist文件。 2018-09-09 14:17:28.909834 + 0800   App [1776:93209]无法开始加载任务   <038CF619-0F68-4196-8B76-4E70DFD678A8>。<0>,因为它不符合   遵守ATS政策2018-09-09 14:17:28.910996 + 0800 App [1776:93173]   NSURLConnection完成,错误-代码-1022   /Users/MyName/Library/Developer/CoreSimulator/Devices/29504265-F97B-4145-AA09-7E8AD66C5F99/data/Containers/Data/Application/78C96743-8EFC-4D53-8ABA-D384321D2156/Documents/Exports.pdf   2018-09-09 14:17:29.078694 + 0800 App [1776:93160]任务   <4F930735-1374-4DF3-A72B-F698A2BEA8BB>。<0> HTTP加载失败(错误   代码:-999 [1:89])

这里有什么建议吗?

0 个答案:

没有答案