本例中服务器返回的是base64 字符串,也可以直接返回byte[],看server接口的定义
拿到nsData以后,把pdf写到device
使用webview显示PDF
原文链接:https://www.f2er.com/swift/321428.htmllet 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) }