如何使用 wkwebview 打开 Link 到 PDF
How to open a Link to a PDF with wkwebview
我创建了一个简单的 iOS 应用程序,它使用 WKWebView 打开 URL。在网站上,有一个 link 到 PDF 文档。当我在浏览器上打开该站点时,我可以单击 Link,然后打开 PDF 文档。但是在我的应用程序上,当我点击 link.
时没有任何反应
我该如何解决?我是否必须在 info.plist 中添加一些内容?
SWIFT 3.* & 4.* *
首先您必须将该 pdf 文件下载到您的应用程序中,下载后您必须获取该文件路径,然后该文件路径应按以下方式使用 WKWebView
。
let fileURL = URL(fileURLWithPath: filePathURLData as! String)
//print(fileURL)
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
这里 filePathURLData
是你下载到你的应用程序中的实际文件路径,你必须将其转换为 URL
,然后你需要将该文件加载到 WKWebView
谢谢
希望对您有所帮助。
这将显示 WKWebView
中的任何文件(doc、docx、xlsx、pdf、google 文档、页面和任何文本文件)
我有同样的问题,发现 pdf 文件使用的是不安全的 http
。因此该应用程序拒绝打开它。
尝试检查 https
link 看看它是否有效。
还有 WKWebView
,您无需下载 pdf 文件即可打开它。直接加载 url 即可,即
webView.load(URLRequest(url: URL(string: "https-pdf-link")!))
您可能在锚标记中使用了 target="_blank"。这会打开一个新的 window 来显示 link。 WKWebView 正在阻止您尝试打开新的 window(至少在默认情况下)。
下面的代码仍然没有创建新的window,而是在当前WKWebView 中打开PDF 等link。
另一个选项似乎是创建一个新的 WKWebView 和 return 它,所以 ios 并在其中打开 link ,但我不希望每次点击都创建额外的视图在 WKWebView 内的网站上。
在你的ViewController.viewDidLoad
webView.uiDelegate = self
然后为委托添加扩展
extension ViewController: WKUIDelegate {
/**
* Force all popup windows to remain in the current WKWebView.
* By default, WKWebView is blocking new windows from being created
* ex <a href="link" target="_blank">text</a>.
* This code catches those popup windows and displays them in the current WKWebView.
*/
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// open in current view
webView.load(navigationAction.request)
// don't return a new view to build a popup into (the default behavior).
return nil;
}
}
Objective C:
NSURL *fileUrl = [[NSURL alloc] initWithString:filePathURLData];
[webView loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];
另一种打开保存在文档目录中的PDF的方法:
NSData *data = [NSData dataWithContentsOfFile:fileURL];
[webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"" baseURL:[NSURL URLWithString:fileURL]];
我创建了一个简单的 iOS 应用程序,它使用 WKWebView 打开 URL。在网站上,有一个 link 到 PDF 文档。当我在浏览器上打开该站点时,我可以单击 Link,然后打开 PDF 文档。但是在我的应用程序上,当我点击 link.
时没有任何反应我该如何解决?我是否必须在 info.plist 中添加一些内容?
SWIFT 3.* & 4.* *
首先您必须将该 pdf 文件下载到您的应用程序中,下载后您必须获取该文件路径,然后该文件路径应按以下方式使用 WKWebView
。
let fileURL = URL(fileURLWithPath: filePathURLData as! String)
//print(fileURL)
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
这里 filePathURLData
是你下载到你的应用程序中的实际文件路径,你必须将其转换为 URL
,然后你需要将该文件加载到 WKWebView
谢谢
希望对您有所帮助。
这将显示 WKWebView
中的任何文件(doc、docx、xlsx、pdf、google 文档、页面和任何文本文件)
我有同样的问题,发现 pdf 文件使用的是不安全的 http
。因此该应用程序拒绝打开它。
尝试检查 https
link 看看它是否有效。
还有 WKWebView
,您无需下载 pdf 文件即可打开它。直接加载 url 即可,即
webView.load(URLRequest(url: URL(string: "https-pdf-link")!))
您可能在锚标记中使用了 target="_blank"。这会打开一个新的 window 来显示 link。 WKWebView 正在阻止您尝试打开新的 window(至少在默认情况下)。
下面的代码仍然没有创建新的window,而是在当前WKWebView 中打开PDF 等link。 另一个选项似乎是创建一个新的 WKWebView 和 return 它,所以 ios 并在其中打开 link ,但我不希望每次点击都创建额外的视图在 WKWebView 内的网站上。
在你的ViewController.viewDidLoad
webView.uiDelegate = self
然后为委托添加扩展
extension ViewController: WKUIDelegate {
/**
* Force all popup windows to remain in the current WKWebView.
* By default, WKWebView is blocking new windows from being created
* ex <a href="link" target="_blank">text</a>.
* This code catches those popup windows and displays them in the current WKWebView.
*/
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// open in current view
webView.load(navigationAction.request)
// don't return a new view to build a popup into (the default behavior).
return nil;
}
}
Objective C:
NSURL *fileUrl = [[NSURL alloc] initWithString:filePathURLData];
[webView loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];
另一种打开保存在文档目录中的PDF的方法:
NSData *data = [NSData dataWithContentsOfFile:fileURL];
[webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"" baseURL:[NSURL URLWithString:fileURL]];