简体   繁体   中英

How to include the js files of my html page in my IOS app?

i'm facing a problem when i'm trying to load an html string in the UIWebView. This is what i'm doing

NSString *temp = [[NSString alloc] initWithFormat:@"<head><script type='text/javascript' src='code39.js'></script><style type='text/css'>#barcode {font-weight: normal; font-style: normal; line-height:normal; sans-serif; font-size: 12pt}</style></head><body><script type='text/javascript'>function get_object(id) {var object = null;if (document.layers) {object = document.layers[id];} else if (document.all) {object = document.all[id];}return object;}get_object('barcode').innerHTML=DrawCode39Barcode('%@',2);</script></body>",[count objectAtIndex:i]];

[mainWebView loadHTMLString:temp baseURL:nil];

so here is the problem:

<script type='text/javascript' src='code39.js'>

how can i add the code39.js file in my application so the my html can access it and load it in UIWebview.

please help...

The simplest solution

Assuming that your app ships with the JavaScript code, and doesn't fetch it over the network, a simple solution is to make your JavaScript code inline instead of an external resource. That is, you can put the contents of code39.js between the <script> tags, just like your other JavaScript code.

Of course, this doesn't work very well if you want to add a lot of code, and further complicates your HTML. Your NSString declaration is going to get (even more) unwieldy very quickly!


Alternative: Add the script as a resource

You can add the JavaScript file as a resource file to be included inside your app bundle. To do this, just add code39.js to your Xcode project.

Note that by default, Xcode will add .js files to your "compile sources" build phase. You don't want this; you want to treat the file as a resource. Thankfully, it's easy to fix:

  1. Select the top-level project to access its settings.
  2. Click on your application target, then choose "Build Phases".
  3. Drag your .js file out of the "Compile Sources" section and into the "Copy Bundle Resources" section.

This will ensure that Xcode copies the script file to the "Resources" directory when you build your app.

Finding the script resource's URL

Once your script is included as a resource, you need to find the URL that points to its location on the filesystem. Fortunately, Cocoa makes this pretty easy to do:

NSURL* url = [[NSBundle mainBundle] URLForResource:@"code39" withExtension:@"js"];

The url you get back is the absolute URL to the code39.js file inside your app bundle. From there, it should be pretty trivial to insert this URL into another string, like so:

NSString* html = [NSString stringWithFormat:@"<script src='%@'>", url];

Once your load your HTML, the UIWebView will have the full URL to your script, and it should execute. This method worked in a test project I just created for iOS 6.0.


(Side note: I strongly suggest keeping your HTML content separate from your source code as well. You can easily adapt the approach above to read the contents of a plain text file into an NSString .)

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.

If the js file is added to the project, hope the below code will work.

[mainWebView loadHTMLString:temp baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

Or -- If the js file received from Server, put the code39.js file in application's document folder.

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *basePath = [paths objectAtIndex:0];
   NSURL *baseURL = [NSURL fileURLWithPath:basePath];
   [mainWebView loadHTMLString:temp baseURL:baseURL];

hope this will solve the issue :)

well well, it is a little more complex. if it is a html file, just use the relative path. if the html is write is objective-c code, u need to setup a http sever, like https://github.com/robbiehanson/CocoaHTTPServer , and use absolute path of js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM