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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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