WriteEatSleepRepeat
WriteEatSleepRepeat

Reputation: 3143

Why UIWebView anchor's target is ignored?

I have a UIWebView embedded within an iPhone app I am developing. I am trying to make the links open in Safari by using target="_blank" on anchors.

I see that the target property is ignored by the UIWebView, links do not open in a new Safari instance. Why is that?

I know that in order to make this work I have to use shouldStartLoadWithRequest.

Upvotes: 2

Views: 834

Answers (2)

Mike M
Mike M

Reputation: 4436

Perhaps you can try something like:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];

Upvotes: 0

sergio
sergio

Reputation: 69037

You should use openURL to make Safari open a web page from your app:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://..."]];

If you want to "intercept" the tap on a given link and open Safari with that URL, you can use, as you say, shouldStartLoadWithRequest. I would suggest to use a custom scheme for the protocol of your URL, so you can differentiate links that should open in Safari from links that can be open in your UIWebView:

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqual:@"safari"])
    [[UIApplication sharedApplication] openURL:[NSURL stringByReplacingOccurrencesOfString:@"safari://" withString:@"http://"]];

 }

In this case, you would specify your urls in the HTML page as "safari://..."

Otherwise, simply call openURL passing the full URL to it.

Upvotes: 2

Related Questions