Skip to content

Instantly share code, notes, and snippets.

@EddyVerbruggen
Last active August 29, 2015 14:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EddyVerbruggen/3edb9f33b29d26c79ec7 to your computer and use it in GitHub Desktop.
Save EddyVerbruggen/3edb9f33b29d26c79ec7 to your computer and use it in GitHub Desktop.
Cordova iOS 3.7.0 handleOpenURL coldstart fix
// I'm not saying this is the best solution, it's just a temp fix which works in my case to solve
// the issue where a passed in url is not propagated to the handleOpenURL js function upon
// coldstart on iOS Cordova 3.7.0.
// Stay tuned on this issue for a better fix: https://issues.apache.org/jira/browse/CB-7606
// replace processOpenUrl in CDVViewController.m by this:
- (void)processOpenUrl:(NSURL*)url pageLoaded:(BOOL)pageLoaded
{
NSString* readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
// for coldstart
if (!pageLoaded) {
pageLoaded = [readyState isEqualToString:@"interactive"];
// for resume
} else if ([readyState isEqualToString:@"loaded"] || [readyState isEqualToString:@"complete"]) {
pageLoaded = true;
}
if (pageLoaded) {
// calls into javascript global function 'handleOpenURL'
NSString* jsString = [NSString stringWithFormat:@"if (typeof handleOpenURL === 'function') { handleOpenURL(\"%@\");}", url];
[webView stringByEvaluatingJavaScriptFromString:jsString];
} else {
// save for when page has loaded
self.openURL = url;
}
}
// or replace it by this one which seems a bit more robust:
- (void)processOpenUrl:(NSURL*)url pageLoaded:(BOOL)pageLoaded
{
if (!pageLoaded) {
// query the webview for readystate
NSString* readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
pageLoaded = [readyState isEqualToString:@"loaded"] || [readyState isEqualToString:@"complete"];
}
if (pageLoaded) {
// calls into javascript global function 'handleOpenURL'
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
NSString* jsString = [NSString stringWithFormat:@"if (typeof handleOpenURL === 'function') { handleOpenURL(\"%@\");}", url];
[webView stringByEvaluatingJavaScriptFromString:jsString];
});
} else {
// save for when page has loaded
self.openURL = url;
}
}
@confile
Copy link

confile commented Jan 5, 2015

The first version is not working on iPhone 5 ios 7.1.1.

@mirko77
Copy link

mirko77 commented Jan 29, 2015

I am not able to make it work with either

@botweb
Copy link

botweb commented Jan 29, 2015

It didn't work with my Sencha Touch powered App. I needed to increase the dispatch time to 500ms.

@jfrumar
Copy link

jfrumar commented Jan 30, 2015

Thanks for this contribution Eddy, and for your input mirko77. We have had to up the dispatch time to 1000ms, and it's working for the iPhone 5 and 6, but the 4 is much slower to launch and doesn't start up the app in time. We could increase the timeout, but it's becoming very noticeable for the users with modern devices.

@kapejod
Copy link

kapejod commented Apr 23, 2015

Instead of messing with dispatch_after, I made a small patch that keeps track of the pageDidLoad callback, see https://gist.github.com/kapejod/85bd38cd74971741ee15

@alexszilagyi
Copy link

@kapejod: can you paste the link of the entire class? Thanks! +1 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment