Thursday, December 18, 2008

More iPhone code, opening some URLs in Safari

I answered a question about this in the iPhone developer forums, so thought others might find it useful too.

If you have a UIWebView in your app to show some web page (which I'm using for all the help pages.) You may still want it to open a new Safari window for other things--like links to the AppStore for other programs. This little bit sees what they web view will open, and if it's a URL on apple.com or an email address, opens that in Safari or the Mail app.

An equivalent of the "myEndsWith" is something you'll still have to do :)


- (BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
//Opens any to apple.com in Safari (so app-store links open right :)
if ([[[request URL] host] myEndsWith:@"apple.com"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
//Mailto opens in email app...
if ([[[request URL] scheme] myStartsWith:@"mailto"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}

No comments: