Enabling Javascript as a Local Resource in Xcode 4 Using UIWebView

I saw this question answered in Xcode 3, but Xcode 4 seems to have changed.

I cannot figure out how to include the javascript file as a local resource for the iPhone App using UIWebView. The compiler tries to interpret the included .js files as code, and not include them in the root application package as a resource for the link in the html file.

In my case, I specifically tried to include a local copy of the javascript file so that part of the UIWebView application could perform certain functions without being connected to the Internet, so I need a local copy ....

Has anyone figured this out for Xcode 4?

Thanks Steve

+4
source share
2 answers

Wow. The hours worked are trying to figure this out, so I finally posted a question. After 10 minutes, I run through a completely unrelated stream, which pointed me in the right direction.

In Xcode 4, in the Project Navigator, click on the top element, the project, then select the target and click on the "Build Phases" tab. This window has Bundle Copy Resources that will allow you to add the .js files that you need in local resources. Now all the html links, such as:

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

will work

+4
source

@gcamp is right, but he forgot to mention that when you call - loadHTMLString: or - loadRequest :. You must specify baseURL for the URL of your bundle resource directory. like this

 // load htmlString into web view [webView loadHTMLString:HTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath] isDirectory:YES]]; 

`` ``

+1
source

All Articles