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)
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 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
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.
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)
}
}
Opening PDF files in WKWebView objective c
You don't need to do this:
TextToBeDisplayed = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&err];
Also in this method you'll get not NSURLRequest, but some kind of text from inside your PDF file trying to transform to NSURLRequest
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:TextToBeDisplayed]];
All you need is this:
NSString *filePath;
filePath = [[NSBundle mainBundle] pathForResource:@"Sample1" ofType:@"pdf"];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
[webView setBackgroundColor:[UIColor whiteColor]];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
Related Topics
How to Make a Mkannotationview Touch Sensitive
Get Device Current Orientation (App Extension)
How to Change a Swiftui Color to Uicolor
Dyld: Library Not Loaded for a Framework Within a Framework
Uipickerview as Inputview of Uitextfield
Delphi Xe4 iOS Can't Connect to Paserver
Google Maps Url Scheme Not Working on iOS 9
iOS 10: How to Show Incoming Voip Call Notification When App Is in Background
Can't Scale Device Window in iOS Simulator
Running Nsurlsession Completion Handler on Main Thread
Facebook App Invites Notification Not Working in iOS
iOS Swift 3:Convert "Yyyy-Mm-Dd'T'Hh:Mm:Ssz" Format String to Date Object
Swift Error: Editor Placeholder in Source File
Admob Sdk - iOS -File Not Found