2

UIWebView在我正在开发的 iPhone 应用程序中嵌入了一个。我正在尝试通过在锚点上使用 target="_blank" 在 Safari 中打开链接。

我看到目标属性被忽略UIWebView,链接没有在新的 Safari 实例中打开。这是为什么?

我知道为了完成这项工作,我必须使用shouldStartLoadWithRequest.

4

2 回答 2

2

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.

于 2012-09-18T11:51:26.570 回答
0

Perhaps you can try something like:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
于 2012-09-18T11:54:24.563 回答