当我点击UIWebView时如何在safari中打开链接?

时间:2013-08-22 07:59:10

标签: html ios objective-c uiwebview safari

当我点击UIWebview(它就像广告显示)时,我正试图在safari中打开一个链接。 下面的代码正在使用,但是当我点击webview在UIWebview中打开一些链接(不是全部)时会发生什么。

- (BOOL)webView:(UIWebView *)webView1 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{


    if (webView1==webview) {

        if (UIWebViewNavigationTypeLinkClicked == navigationType) {

            [[UIApplication sharedApplication] openURL:[request URL]];
            return NO;
        }

        return YES;

    }
}

这里发生的事情是,如果UIWebView中的任何文本链接正确打开,但是如果带有图像的UIWebview然后在相同的UIWebview中打开而不是新的浏览器。

我的当前代码

     - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/files/ad.htm"]]];
    [_webView setBackgroundColor:[UIColor clearColor]];
    [_webView setOpaque:NO];
    _webView.scrollView.bounces = NO;


    }

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
    }

    return YES;
}

when i load the app i can see the ad

When i click on that ad(UIWebview) it opens in same UIWebview instead of a browser

1 个答案:

答案 0 :(得分:0)

似乎这些图像已使用锚标签链接...

试试这个......

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    static NSString *reguler_exp = @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])[.])+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$";
//Regular expression to detect those certain tags..You can play with this regular expression based on your requirement..

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reguler_exp];
//predicate the matched tags.

    if ([resultPredicate evaluateWithObject:request.URL.host]) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO; 
    } else {
        return YES; 
    }
}

编辑:第一次不允许调用此委托方法。添加一些第一次加载webview时不会调用的逻辑。

SOP的新要求,见下面的评论

如何检测uiwebview上的触摸,

1)将tapgesture添加到你的webview中。(之前在你的viewcontroller.h中添加UIGestureRecognizerDelegate)

示例:

UITapGestureRecognizer* singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTouchesRequired=1;
singleTap.delegate=self;
[webview addGestureRecognizer:singleTap];

2)然后添加此委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    //return YES,if you want to control the action natively;
    //return NO,in case your javascript does the rest.
}

注意:如果你想检查触摸是本机的还是htmml(javascript),请查看关于处理javascript事件等的漂亮的小教程here ....希望这有助于你..