Thursday 8th September, 2011
Using OpenLayers with PhoneGap on iOS: On iOS, the web view that PhoneGap provides doesn't handle about:blank. I was playing with OpenLayers and this caused the map to just not appear and the following error to appear in the console:

PhoneGapDelegate::shouldStartLoadWithRequest: Received Unhandled URL about:blank

To fix this is a two-step procedure (one which will work for other things which require about:blank, too). Firstly, create an html file in the www folder called blank.html, which contains something along the lines of:

<html>
<body>
</body>
</html>

Secondly, look in the AppDelegate.m class under the Classes group in your Xcode project. There will be a method which looks like this:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}

This method is where one is able to rewrite requests on their way through. To rewrite about:blank to point to the blank.html file, it is first necessary to find the path to the www folder and thus to blank.html:

NSString* app = [[NSBundle mainBundle] resourcePath];
NSString* blankpath = [NSString stringWithFormat: @"%@/www/blank.html", app];
NSURL* blankurl = [NSURL fileURLWithPath:blankpath];

And then to generate a new, empty request to pass through to about:blank. As about:blank should literally be that and shouldn't attempt any processing, one doesn't need to worry too much about loss of state.

NSURLRequest* req = [NSURLRequest requestWithURL: blankurl];

Thus, the whole method should look like:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
        NSURLRequest* req = request;
       
        if ([[[request URL] absoluteString] isEqualToString: @"about:blank"]) {
                NSLog(@"Rewriting about:blank");
                NSString* app = [[NSBundle mainBundle] resourcePath];
                NSString* blankpath = [NSString stringWithFormat: @"%@/www/blank.html", app];
                NSURL* blankurl = [NSURL fileURLWithPath:blankpath];
       
                req = [NSURLRequest requestWithURL: blankurl];
        }

        return [ super webView:theWebView shouldStartLoadWithRequest:req navigationType:navigationType ];
}

This should allow web components that rely on about:blank to work correctly. It's trivial to extend this to make any about: url return a blank page - change the if() line to use the hasPrefix: selector:

if ([[[request URL] absoluteString] hasPrefix: @"about:"]) { ...

posted by Rob Mitchelmore, 15:09 (anchor)
June 2015May 2015April 2015June 2014
January 2014November 2013October 2013July 2013
April 2013March 2013January 2013November 2012
older posts