Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>The simplest solution</h3> <p>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 <code>code39.js</code> between the <code>&lt;script&gt;</code> tags, just like your other JavaScript code. </p> <p>Of course, this doesn't work very well if you want to add a lot of code, and further complicates your HTML. Your <code>NSString</code> declaration is going to get (even more) unwieldy very quickly!</p> <hr> <h3>Alternative: Add the script as a resource</h3> <p>You can add the JavaScript file as a resource file to be included inside your app bundle. To do this, just add <code>code39.js</code> to your Xcode project.</p> <p>Note that by default, Xcode will add <code>.js</code> files to your "compile sources" build phase. You <strong>don't</strong> want this; you want to treat the file as a resource. Thankfully, it's easy to fix:</p> <ol> <li>Select the top-level project to access its settings.</li> <li>Click on your application target, then choose "Build Phases".</li> <li>Drag your <code>.js</code> file out of the "Compile Sources" section and into the "Copy Bundle Resources" section.</li> </ol> <p>This will ensure that Xcode copies the script file to the "Resources" directory when you build your app.</p> <h3>Finding the script resource's URL</h3> <p>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:</p> <pre><code>NSURL* url = [[NSBundle mainBundle] URLForResource:@"code39" withExtension:@"js"]; </code></pre> <p>The <code>url</code> you get back is the absolute URL to the <code>code39.js</code> file inside your app bundle. From there, it should be pretty trivial to insert this URL into another string, like so:</p> <pre><code>NSString* html = [NSString stringWithFormat:@"&lt;script src='%@'&gt;", url]; </code></pre> <p>Once your load your HTML, the <code>UIWebView</code> 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.</p> <hr> <p>(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 <code>NSString</code>.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload