1

I'm trying to manually inject the javascript for my Backbone app into a UIWebview (in order to save the client from downloading a 1MB JS file when the app boots).

The code below works perfectly fine in DEBUG builds of the app, but as soon as build an Ad Hoc release build and test that out, the app doesn't load properly. I assume it is a JS error somewhere but I don't know how to debug a UIWebview running in a release build (the Safari dev tools only work in Debug as far as I could tell).

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Inject the JS
    NSLog(@"Injecting JS 1 from disk");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"backbone_application" ofType:@"js"];
    NSString *code = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSString *jsCode = [NSString stringWithFormat:@"var script=document.createElement('script'); script.type='text/javascript'; script.text=\"%@\"; document.head.appendChild(script);", code];
    [self.webPortal stringByEvaluatingJavaScriptFromString:jsCode]
}

Any ideas on why this would be failing in RELEASE mode? Any suggestion on how to test for UIWebview javascript errors in a release build?

3 Answers 3

1

Try removing the following line:

NSString *jsCode = [NSString stringWithFormat:@"var script=document.createElement('script'); script.type='text/javascript'; script.text=\"%@\"; document.head.appendChild(script);", code];

So you'll have this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Inject the JS
    NSLog(@"Injecting JS 1 from disk");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"backbone_application" ofType:@"js"];
    NSString *code = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webPortal stringByEvaluatingJavaScriptFromString:code]
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are settings on both the project and target of your code for each build mode. It sounds to me that you have one or more settings for DEBUG mode that are not the same as RELASE mode. Double check to see if any setting is different, other way to prove this is to create an Archive for Ad Hoc distribution in DEBUG mode (You can edit that on your Scheme Editing Dialog.) If this new .IPA runs as expected then for sure you have different settings for DEBUG/RELEASE.

Hope this helps,

6 Comments

I tried 2 things so far: 1) Setting the Run App's Build Config to Release, but I get an Xcode error: "Could not launch 'MyApp'; failed to get the task for process 4910"; 2) Setting the Archive's Build Config to Debug and creating and installing the IPA file on the device through iTunes. It starts installing then pops up "iTunes Sync: 'MyApp' failed to install." So running 'the opposite' build config doesn't appear to be possible.
Sad to hear that. To understand better what may be the problem, you said it runs fine on DEBUG builds How are you testing this? with the simulator or directly to a device?. Then you are generating an .IPA with RELEASE mode and when trying to install this on the same device over iTunes fails, does the app runs but seems not to load the JS or it does not work at all?
Thanks for the quick reply! Yes, it runs fine in a debug build. By that I mean, upon app boot up, I load the home page (which doesn't include any JS), upon load finished, I inject the JS (which includes JS libraries + our own functions, etc), then call App.start() to start the backbone app. This is all in development. When I generate an .IPA in Release mode and install through iTunes, I run the app normally, it does the same thing (loads the page, injects JS, & calls App.start()) but nothing happens. Since it's a release build, I can't debug the JS.
PS, I just went through all of the Build Settings one by one and copied the value from RELEASE and used it in the DEBUG section..hoping that one of the values in RELEASE was causing the issue, but no luck.
I see. Other thing that may be causing this issue is the way you are calling the 'backbone_application' file. Remember that the filesystem is case-sensitive so check if the file name is spelled correctly. Other thing you may check is the path of your resource. If your are using subdirectories you should be using a more precise method like NSString * path = [[NSBundle mainBundle] pathForResource:@"backbone_application" ofType:@"js" inDirectory:@"directory/subdirectory"];
|
1

Did you try turning on strict mode in debug?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.