WkWebView does not load links to pdfs
There are a few things you can check:
Verify that the
<a href
links doesn't containtarget="_blank"
attribute since WKWebView doesn't know how to open the link in a new tab. see https://stackoverflow.com/a/25853806/810466 for how to work around that.Check if the link is HTTPS or update the
App Transport Security Settings
with theAllow Arbitrary Loads
optionMake sure that you start the loading request only after adding the WKWebView to the view hierarchy in
didMoveToParentViewController:
since it may make javascript to fail if it tries to run outside the view hierarchyImplement the WKWebView NavigationDelegate methods and make sure you return
WKNavigationActionPolicyAllow
when deciding the policy for the request
WKWebView not loading pdf
If you are directly trying to open the pdf from URL into WKWebView it will not work because of restriction added to WKWebView for device protection.
You can refer this article How to open a Link to a PDF with wkwebview for more information.
How to open a Link to a PDF with wkwebview
SWIFT 3.* & 4.* *
First you have to download that pdf file into your app, after downloading you have to get that file path, then that file path should be use like following way in WKWebView
.
let fileURL = URL(fileURLWithPath: filePathURLData as! String)
//print(fileURL)
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
Here filePathURLData
is your actual file path which you have downloaded into your app, you have to convert this into URL
, then you need to load that file into WKWebView
Thanks
Hope this will help you.
This will show any file in the WKWebView
(doc, docx, xlsx, pdf, google doc, pages & Any textfile)
Why am I not able to load valid PDF file with WKWebView?
The code you have provided directly does not work, and leads to the following error
WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=WebKitErrorDomain, code=102
I did not find any official apple documentation for this specific error but a simple search points to some kind of interruption.
As an alternative, you can try to load data from the URL:
if let data = try? Data(contentsOf: url) {
self.webView.load(data, mimeType: "", characterEncodingName: "", baseURL: url)
}
But this leads to the weird page you have shown in your question.
Here, the web view does not know that you are trying to show a PDF. Simply providing the content type fixes the issue.
if let data = try? Data(contentsOf: url) {
self.webView.load(data, mimeType: "application/pdf", characterEncodingName: "UTF8", baseURL: url)
}
So, you need to make sure the web view knows what type of content you're going to load.
How you would do that for various file types you want to support is a different question you need to figure out.
iOS 13 WKWebView not showing pdf file anymore
I figured out that the response' content-type was "application/octet-stream" instead of "application/pdf"
So I load the WKWebView
as:
if let myURL = URL(string:"somefileurl.pdf") {
if let data = try? Data(contentsOf: myURL) {
webViewPdf.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL)
}
}
WKWebView open PDF in external browser
You can do something like this
extension ViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.request.url ?? "")
if let url = navigationAction.request.url?.absoluteString{
}
decisionHandler(.allow)
}
}
And don't forget to assign
webView.navigationDelegate = self
The above method will get called when ever webview tries to load a new url and as per your comment and updated question you want to check the mime type of url. For that you can refer to
This SO Answer
Library
Why is WKWebView not opening links with target= _blank ?
My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar behavior like UIWebView which always open new window in the current frame.
Implement the WKUIDelegate
delegate and set it to _webview.uiDelegate
. Then implement:
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
Related Topics
Type Conversion When Using Protocol in Swift
How to Configure Contextmenu Buttons For Delete and Disabled in Swiftui
Uiwebview Dynamic Content Size
Is Swiftui Backwards-Compatible With iOS 12.X and Older
In Swift, What Does the ! Symbol Mean in a Function Signature
Swiftui: Pop to Root View When Selected Tab Is Tapped Again
Extension May Not Contain Stored Property But Why Is Static Allowed
Passing Data Between Tab Viewed Controllers in Swift
Whats the Swift Animate Withduration Syntax
Can You Override Between Extensions in Swift or Not? (Compiler Seems Confused!)
Swift Optional Inout Parameters and Nil
Swift Spritekit Adding Button Programmatically
Retrieving Image from Firebase Storage Using Swift
How to Check Two Instances Are the Same Class/Type in Swift
Spritekit Physics in Swift - Ball Slides Against Wall Instead of Reflecting
The "Prefs" Url Scheme Is Not Working in iOS 10 (Beta 1 & 2)