iphone之点击UIWebView中链接调用Safari打开目标链接

发布时间:2014-10-23 23:26:53
来源:分享查询网

(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; } 二、 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {     if (navigationType==UIWebViewNavigationTypeLinkClicked) {         NSString *currentUrl=request.URL.absoluteString;         [self applicationWebLoad:currentUrl];         return NO;     }     return YES; } -(void)applicationWebLoad:(NSString *)urlString {     UIApplication *application=[UIApplication sharedApplication];     NSURL *url =[NSURL URLWithString:urlString];     if ([application canOpenURL:url]) {         [application openURL:url];     }     else     {         NSLog(@"open url failed");     } } *需要注意的是,-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 这个方法,会在发起请求 loadRequest后调用,而且调用的次数也不是一次,直到加载完毕  返回值为no表示不加载 一般情况我们把加载方法写在了viewWillAppear中,点击webview中的链接,调用safari,当我们回到我们的应用中,当前WebView就会重新加载到别的页面(点击后的),而这是通常是不允许的,所以就可以使用上面的方法。

返回顶部
查看电脑版