I am using PhoneGap for an iOS project and everything is working except I want to detect a click of the logout link in our web app and intercept it with local logout logic. I am able to detect the logout link, but the logic below ALSO thinks it is the logout page when about:blank fires in PhoneGap’s UIWebView. Any ideas what I’m doing wrong?
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// If we detect the logout.jsp we need to go back to the login form and reset the credentials
if ([[url path] rangeOfString:@"logout.jsp"].location == NSNotFound) {
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
return YES;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
else {
// have PhoneGap go back to index page
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[theWebView loadRequest:requestObj];
return NO;
}
}
EDIT:
I haven’t solved why this is happening, so for now I have added this workaround in the main else clause:
if ([[url path] rangeOfString:@"about:blank"].location != NSNotFound) {
return YES;
}
The bounty will go to someone who can figure out why I even need that workaround when the initial IF is only checking for the logout.jsp string in the url and about:blank should never match that, but does in this case.
If the request url is “about:blank”, then
[url path]returns nil.So
[nil rangeOfString:@"logout.jsp"].locationreturn 0 and NSNotFound != 0.That’s why the sentence
([[url path] rangeOfString:@"about:blank"].location != NSNotFound)is TRUE.