Swift 保存pdf并在webview显示

前端之家收集整理的这篇文章主要介绍了Swift 保存pdf并在webview显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本例中服务器返回的是base64 字符串,也可以直接返回byte[],看server接口的定义
let nsData = NSData(base64Encoded: data!!)


拿到nsData以后,把pdf写到device
let theData = ServiceProxy().CallGetPayslipFile(recordId: payslip.recordId)//"204586"
        var docURL = (FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)).last as NSURL?
        
        docURL = docURL?.appendingPathComponent( "myPayslip.pdf")! as NSURL?
        
        //Lastly,write your file to the disk.
        theData.write(to: docURL! as URL,atomically: true)


使用webview显示PDF
func loadPDF(filename: String) {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0]
        let filePath = "\(documentsPath)/\(filename)"
        let url = NSURL(fileURLWithPath: filePath)
        let urlRequest = NSURLRequest(url: url as URL)
        
        webView.loadRequest(urlRequest as URLRequest)
    }
原文链接:https://www.f2er.com/swift/321428.html

猜你在找的Swift相关文章