将base64字符串显示为webview的PDF格式

时间:2017-07-19 09:36:43

标签: swift pdf webview

我有base64字符串代表PDF。 我想在Webview中显示它,但我不知道如何做得好。

我该如何处理?我是否需要先将String转换为数据?

1 个答案:

答案 0 :(得分:4)

请尝试以下代码。

if let decodeData = NSData(base64EncodedString: base64String, options: .IgnoreUnknownCharacters) {
    yourWebView.loadData(decodeData, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: NSURL(fileURLWithPath: ""))
} // since you don't have url, only encoded String

Swift 4.0

if let decodeData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters) {
        yourWebView.load(decodeData, mimeType: "application/pdf", textEncodingName: "utf-8", baseURL: URL(fileURLWithPath: ""))
} // since you don't have url, only encoded String