Skip to content

Instantly share code, notes, and snippets.

@EddyVerbruggen
Last active August 29, 2015 14:10
Show Gist options
  • 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;
}
}
@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