我们有一个网站。在某些情况下,用户可以使用内置浏览器从Facebook App或VKontakte App打开。我想在Safari或其他浏览器中打开一个链接。但不是在这个WebView中。
任何可以告诉WebView的HTML属性或其他东西本身都不能打开此链接吗?
更新:我不是App开发人员。我是网站开发人员。我没有任何访问Facebook App的权限。我需要强制Facebook App打开不在其内置浏览器中的链接。
答案 0 :(得分:4)
您可以使用UIWebViewDelegate:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(UIWebViewNavigationTypeLinkClicked == navigationType /*you can add some checking whether link should be opened in Safari */) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
答案 1 :(得分:0)
Web视图包含一些委托方法,用于从该委托方法中查找单击事件,您需要像这样调用URL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
这是在单击Web视图中的任何链接时将触发的委托方法
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
{
[[UIApplication sharedApplication] openURL:[request URL]];
}
}
只需将网址与URLRequest区分开来。
答案 2 :(得分:-1)
如果您设置了UIWebViewDelegate
,则可以使用方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
使用此方法,我们可以阻止或允许在UIWebView
或移动浏览器中打开某些链接/网址。
所以我们可以做类似
的事情- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// We only want to block links
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *url = [[request URL] absoluteString];
if([url isEqualToString:@"http://www.google.co.uk"]) {
// If the requested url is a link and is http://www.google.co.uk block it from loading in the UIWebView and load it in the mobile browser.
if([[UIApplication sharedApplication] canOpenUrl:[request URL]]) {
// We should be checking to make sure we can open it first, for whatever reason we might not be able to and we don't want to give the user a bad journey.
[[UIApplication sharedApplication] openURL:[request URL]];
// We also need to tell our UIWebView not to do anything so we need to return NO.
return NO;
}
}
}
// If all is OK and we are happy for any other link to be included then just return YES and continue.
return YES;
}
如果您想阻止所有链接,只需删除isEqualToString:
if statement
,或者只需将else if
添加到if statement
即可阻止某些链接。此代码执行以下两项操作之一:它会在UIWebView
中加载点击的链接网站,或者如果网站通过if statements
,则会在移动浏览器中加载网站。
<强>更新强>
事实证明,用户没有objective-c
他们正在执行html
的事情,这不是原始问题,但是因为这可以帮助用户将来我会把答案留在这里。
修改强>
由于用户正在使用html
尝试将用户引导至移动浏览器,因此他们所寻找的内容称为URL方案。不幸的是,我不相信会有iOS 7
的任何URL方案会打开safari移动浏览器。
之前已经问过这个问题,所以我不会再坐在这里再说一遍我只会引导你回答问题Open Mobile Safari from a Link in a WebView
答案 3 :(得分:-1)
//codigo para abrir no safari
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL =[ [ request URL ] retain ];
if ( ( [ [ requestURL scheme ] isEqualToString: @”http” ] || [ [ requestURL scheme ] isEqualToString: @”https” ] || [ [ requestURL scheme ] isEqualToString: @”mailto” ])&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) )
{ return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; } [ requestURL release ]; return YES;
}