【问题标题】:How can i open a link in safari when i click UIWebView?当我单击 UIWebView 时,如何在 Safari 中打开链接?
【发布时间】:2013-08-22 07:59:09
【问题描述】:

当我单击 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;
}

【问题讨论】:

  • 那么你在webview中加载的html文件是本地的吗?
  • @iOSCoder 是的,实际上只有一个 url,如果它不起作用,那么它将从本地文件加载
  • @Popeye 有没有办法通过点击uiwebview打开safari
  • 为什么这个 if conition "if (webView1==webview) "?
  • 这是什么意思?它在 UIWebview 中打开一些链接(不是全部)。

标签: html ios objective-c uiwebview safari


【解决方案1】:

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

试试这个...

- (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; 
    }
}

编辑:不允许第一次调用此委托方法。添加一些逻辑,使其在第一次加载 web 视图时不会被调用。

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),请查看漂亮的小教程here,关于处理 javascript 事件等......希望这会有所帮助你..

【讨论】:

  • 有什么方法可以知道webview被点击了吗?
  • 由你决定..你需要的是..这是苹果工程师做的,所以还不错:-)
  • @请检查我更新的问题..我的代码崩溃了
  • 我的错误它没有崩溃但它没有打开浏览器..但我可以看到那个 NSLog(@"tap");我冲刺
猜你喜欢
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2011-12-01
  • 2016-04-16
相关资源
最近更新 更多