我UIWebView
在我正在开发的 iPhone 应用程序中嵌入了一个。我正在尝试通过在锚点上使用 target="_blank" 在 Safari 中打开链接。
我看到目标属性被忽略UIWebView
,链接没有在新的 Safari 实例中打开。这是为什么?
我知道为了完成这项工作,我必须使用shouldStartLoadWithRequest
.
我UIWebView
在我正在开发的 iPhone 应用程序中嵌入了一个。我正在尝试通过在锚点上使用 target="_blank" 在 Safari 中打开链接。
我看到目标属性被忽略UIWebView
,链接没有在新的 Safari 实例中打开。这是为什么?
我知道为了完成这项工作,我必须使用shouldStartLoadWithRequest
.
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.
Perhaps you can try something like:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];