Uiwebview Local Html Javascript File

Posted on by
Uiwebview Local Html Javascript File Average ratng: 3,7/5 6328 reviews

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.

  1. Javascript New File
  2. Javascript In Html
  3. Uiwebview Local Html

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.

Active1 year, 4 months ago

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.

RedBlueThing
34.6k17 gold badges89 silver badges116 bronze badges
Matt PalmerleeMatt Palmerlee
1,9182 gold badges23 silver badges26 bronze badges

8 Answers

This is how to load/use a local html with relative references.

  1. 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'.
  2. Select the 'create folder references.' option.
  3. 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

clozach
3,9385 gold badges35 silver badges49 bronze badges
sdbrainsdbrain
3,0351 gold badge11 silver badges9 bronze badges
Ahmad
8,6315 gold badges38 silver badges65 bronze badges
WelesWeles

I crammed everything into one line (bad I know) and had no troubles with it:

PengOnePengOne
44.5k17 gold badges118 silver badges145 bronze badges

I simply do this:

Where 'index.html' relatively references images, CSS, javascript, etc.

Old McStopherOld McStopher
4,1259 gold badges53 silver badges82 bronze badges
Html

Swift 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.

fishinear
4,5572 gold badges24 silver badges75 bronze badges
ThisIsNotMeThisIsNotMe
Local

I'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 Palmerlee
1,9182 gold badges23 silver badges26 bronze badges
skornos

Javascript New File

skornos
1,4441 gold badge16 silver badges26 bronze badges

Javascript 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.

Bently BoboBently Bobo

Uiwebview Local Html

Not the answer you're looking for? Browse other questions tagged iosuiwebview or ask your own question.