Uiwebview Local Html Javascript File
What are the limitations to JavaScript in a UIWebView? JavaScript in UIWebView? But if I save this as a text file and open it with my Mac Safari browser it.
How to Load a Local.CSS File & JavaScript Resources Using iPhone UIWebView Class. To use relative paths or files in UIWebView, you have to load the HTML into the. In this post I will show you how to load local html file inside a webview follow these four simple steps. This is a continuation of the post Getting started with webview in chrome apps.
I have a very simple iOS app with a uiwebview loading a very simple test page (test.html):
I load this test.html file into my web view:
This works fine if I reference the image without the relative path and put the referenced image in the root path under Targets -> Copy Bundle Resources within XCode, however I can't get it to work with the relative path as shown in my html file. There must be a way to do this, I have lots of images, css, javascript files that I want to load into the webview and I would like not to have to have everything in the root and have to change all the references in my web app.
RedBlueThing8 Answers
This is how to load/use a local html with relative references.
- Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options 'create groups for any added folders' and 'create folders references for any added folders'.
- Select the 'create folder references.' option.
Use the below given code. It should work like a charm.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@'index' ofType:@'html' inDirectory:@'www']];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
Now all your relative links(like img/.gif, js/.js) in the html should get resolved.
Best amigaos 3.9 install 2017 and full version 2017. This way you'll be sure that the value has been applied.
Swift 3
clozachI crammed everything into one line (bad I know) and had no troubles with it:
PengOnePengOneI simply do this:
Where 'index.html' relatively references images, CSS, javascript, etc.
Old McStopherOld McStopherSwift answer 2.
The UIWebView Class Reference advises against using webView.loadRequest(request):
Don’t use this method to load local HTML files; instead, use loadHTMLString:baseURL:.
In this solution, the html is read into a string. The html's url is used to work out the path, and passes that as a base url.
fishinearI've gone back and tested and re-tested this, it appears that the only way I can get it to work (since I have some files in the img folder and some in js/css, etc..) is not to use a relative path to my image in the html and have everything referenced to the bundle folder. What a shame :(.
Matt PalmerleeMatt PalmerleeJavascript New File
skornosJavascript In Html
In Swift 3.01 using WKWebView:
This adjusts for some of the finer syntax changes in 3.01 and keeps the directory structure in place so you can embed related HTML files.