Loading JavaScript into a Uiwebview from Resources

Can't load JavaScript from Resource in UIWebView

The problem was that XCode was showing me a warning:

warning: no rule to process file
'$(PROJECT_DIR)/html/orientation.js'
of type sourcecode.javascript for
architecture i386

This answer contains the solution. Basically, I had to tell XCode to not try and build the JavaScript files, and manually drag them to the "Copy Bundle Resources" directory.

iPhone UIWebView local resources using Javascript and handling onorientationChange

The answer can be traced from the warning I was getting in XCode:

warning: no rule to process file '$(PROJECT_DIR)/html/orientation.js' of type sourcecode.javascript for architecture i386

XCode setup *.js javascript as some type of source code needed to be compiled in the application when I wanted to include it as a resource so I solved it by doing 2 things.

  1. Select .js file and in the "Detail" view unselect the bullseye column indicating it is compiled code
  2. In the "Groups & files" view expand the "Targets" tree and expand the application then go to "Copy Bundle Resources" and drag the *.js files into it

The Apple dev forums has a posted solution:

  • UIWebView and JavaScript

It appears that you are getting bit by
the "Xcode thinks that .js are things
to be compiled" feature. Basically,
Xcode thinks that the script should
somehow be run or compiles, so marks
it as part of the source code. Source
code, of course, is not copied into
the resources.

So you need to do two things - select
the .js file in your project, and turn
off the checkbox that indicates that
it is compiled (the "bullseye"
column). If you don't do this, you'll
get a warning in your build log about
being unable to compile the file
(which should be your first warning -
always try to figure out and and
correct any and all warning that
appear in your build).

Then drag it and drop it in the
target's "Copy Bundle Resources".

Run javascript from local resources using UIWebView

either this is a very simple question, or I didn't understand you but you can add Javascript code to HTML using a simple <script> tag like this:

<script type="text/javascript" src="MY_JAVASCRIPT_FILE.js"></script>

Replace "MY_JAVASCRIPT_FILE.js" with the path of your Javascript file relative to the HTML file. (if they are in the same directory, no path is required, just write the file name).

If this is not the answer to your question please let me know. Thanks.

UIWebView doesn't load external JavaScript file

I have found that Xcode thinks that the js file needs to be compiled.

Open the project in Xcode. Go to "Targets" and look in "Compile Sources". If you see your js file in that folder, move it to the "Copy Bundle Resources" folder and delete it from the "Compile Sources" folder.

Delete the app from your iPhone if it is installed already for testing. Go to the Build menu in Xcode and Clean all Targets. Then recompile.

Check now to see if your HTML file actually sees your js file now.

Linda

Load resources from relative path using local html in uiwebview

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.

Swift 3

    if let path = Bundle.main.path(forResource: "dados", ofType: "html", inDirectory: "root") {
webView.load( URLRequest(url: URL(fileURLWithPath: path)) )
}


Related Topics



Leave a reply



Submit