https://issues.apache.org/jira/browse/KARAF-5376
我正在将base64转换为本地的pdf文件。
func saveBase64StringToPDF(_ base64String: String)
{
guard
var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last,
let convertedData = Data(base64Encoded: base64String)
else {
//handle error when getting documents URL
return
}
documentsURL.appendPathComponent("yourFileName.pdf")
do {
try convertedData.write(to: documentsURL)
} catch {
//handle write error here
}
print(documentsURL)
}
从上述方法中,我正在获取路径pdf文件
file:/// Users / captivatesoft / Library / Developer / CoreSimulator / Devices / 63D1814B-1010-4694-883E-D2EE75C386DD / data / Containers / Data / Application / 1F39B567-F470-4B7E-9A9C-4723ADFDC428 / Documents / yourFileName.pdf
是否可以下载该pdf文件或在Safari中显示pdf?
答案 0 :(得分:1)
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent( "example.pdf") as URL
let data = try! Data(contentsOf: pdfURL)
let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
view.addSubview(webView)
答案 1 :(得分:0)
要在Safari中打开PDF,只需执行
UIApplication.shared.openURL(URL(string: <the link for the PDF>))
现在要从URL下载PDF或任何内容,您只需
let pdfData = try? Data(contentsOf: URL(string: <the link for the PDF>))
注意:try Data(contentsOf: url)
将阻塞线程,因此您可能想要在其他线程上执行此操作:
DispatchQueue.global.async { [weak self] in
if let pdfData = try? Data(contentsOf: url) {
// Now you can do anything with the data
}
}
答案 2 :(得分:0)
首先,您无法在iOS中下载pdf文件,这意味着您无法将pdf文件保存在iBook或相册中。
您可以从文件URL提取pdf文件数据,然后可以执行以下操作:
使用WebView:
if let url = URL(string:string) {
webView.load(URLRequest(url: url))
}