最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to include the js files of my html page in my IOS app? - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Oct 17, 2012 at 5:01 Prateek RajPrateek Raj 3,9966 gold badges41 silver badges51 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

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 plicates 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 "pile 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 plex. 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./robbiehanson/CocoaHTTPServer, and use absolute path of js.

发布评论

评论列表(0)

  1. 暂无评论