Last active
August 29, 2015 14:10
-
-
Save EddyVerbruggen/3edb9f33b29d26c79ec7 to your computer and use it in GitHub Desktop.
Cordova iOS 3.7.0 handleOpenURL coldstart fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
I am not able to make it work with either
It didn't work with my Sencha Touch powered App. I needed to increase the dispatch time to 500ms.
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.
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
@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
The first version is not working on iPhone 5 ios 7.1.1.