PDF file not shown in iPad webView - swift

I have a Swift application that opens a PDF file in webView with the function webView.load(request).
It perfectly works at debug time with the simulator, but when launched in the real device (an iPad) the PDF file is not shown.
The app does not set any error.
Any help is welcome

This might be so obvious question: Is your iPad connected to internet?
Then maybe it's because your link isn't and "https://...." but just an "http://" and maybe the webview won't open it because of that

The PDF file is located in the default documents folder.
Any loading to the webView is working, but not the loading of a PDF file.
The strange is that the same code works in the simulator.

For Swift 3 use this
let url = URL(fileURLWithPath: filePath)
let urlRequest = URLRequest(url: url)
webView?.loadRequest(urlRequest)

Related

device webKitView not loading local images but simulator load the images

I have a html file located locally in the Library directory of the device and some images in the same directory. so when I open the webkitview in a simulator it loads the images perfectly but when I use a device the html page is loaded but the images are not loaded.this is my html :
<html><head><script src='./test.js'></script></head><body><img src='./sample_attachment.jpg' alt='Girl in a jacket' /><body><html>
and this is how I load the web view:
webView.loadFileURL(path, allowingReadAccessTo: path)
I have also enabled the Allow Arbitrary Loads in info.plist.
Thank you for your help !
I found the solution it can help anyone of you:
the first path in webView.loadFileURL(firstPath, allowingReadAccessTo: secondPath) is the path to the html file and the second path is the path to resources that you are allowed to open, the problem I was putting the same path that's why only the html page is loaded (I don't know why the simulator load the ressources correctly) but when I put the path to the directory in the second path it can access all the resources and load them perfectly.

swift 4 - Can not show pdf in a UIWebView

I am working on the new version of swift 4 and i have the next issue, i can not show a ".pdf" on a UIWebView. First i migrated my project to the new version of Swift, then i had to solve the issue of "Set Swift 3 #objc inference to Default", then when i run my project just some of the PDFs i set when i was working on Swift 3 are showing, i added new ones after upgrade the swift but does not show it on the UIWebView. This is my code to show the pdf files:
func showCarPdf() {
print(carPdfView!)
if let pdf = Bundle.main.url(forResource: carPdfView!, withExtension: "pdf", subdirectory: nil, localization: nil) {
let req = NSURLRequest(url: pdf)
webView.loadRequest(req as URLRequest)
}
}
Does any one know whats happend? Why i can only show the files that i set on swift 3 and not the new ones added with the new version. Thanks a lot guys.
From your question I understand that you are adding new pdf file... right?
If so...
By default when a resource(images, pdfs, etc...) is added in Xcode 9, it is not getting added to Target.
If you add a new resource make sure, it is set to Target as well. Hope you know how to check whether a resource is added to Target. You can also verify whether a resource is added from the 'Copy Bundle Resources"

Swift : WKWebView not showing image on device

I am loading a html file to WKWebview from local everything is working fine but it is not showing image on iPhone.
This is the code i am using to load a html file
do{
let fileName = try String(contentsOf: destinationURLForFile)
print("fileName is equal to\(fileName)")
webView.loadHTMLString(fileName, baseURL: destinationURLForFile)
}catch{
}
Trying to fix since hours. Any suggestions would be helpful!
I had a very similar issue yesterday, you need to tell the build to copy this file to the device as part of the bundle.
Make sure sure you have the HTML file in the Copy Bundle Resources part of the build settings. Just go to the project settings > build phases > copy bundle resources and if your HTML file is not listed there, click the + button and add it.
This should solve your issue, if not could you please provide more information, are you getting any errors, does the file actually exist?

OSX Sandbox WKWebView Load Local File Inside

I have a WKWebView tied to an NSOpenPanel. I let the user select a file, but I am having trouble loading the file. When the app was not sandboxed I didn't have any problems, but once I turned on sandboxing I couldn't get it to work on OSX < 10.11. On OSX 10.11 I can use webView.loadFileURL(url, allowingReadAccessToURL: url), but it isn't available on < 10.11. What is the best way to load files from NSOpenPanel in a sandboxed osx swift app? Thanks!
NSURL from your NSOpenPanel into NSData with initWithContentsOfURL
then get a NSString out of this NSData
then use
WKWebView loadHTMLString(_ string: String,
baseURL baseURL: NSURL?)

Can't open PDF file located in server in WebView on android device

I would like to open a PDF file that located in the server (Here)
in a specific place in my application.
I saw a couple of example how to do it with a webview, but no one works for me.
This is part of my code that should preview the pdf inside a WebView :
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://mes.mes-ltd.com/itay.pdf");
The solution is just a Blank webview.
Thanks for help!
You can't open a PDF directly in the WebView, it just doesn't support this. You'll need to do something like the solution outlined here.