2013-08-22 139 views
2

我試圖在Safari中打開一個鏈接,當我點擊UIWebview(就像廣告顯示)。 以下代碼正在使用,但是當我點擊webview時,它在UIWebview中打開了一些鏈接(並非全部)時發生了什麼。當我點擊UIWebView時,如何打開safari中的鏈接?

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

+0

所以你在webview中加載的html文件是本地的? –

+0

@iOSCoder實際上是一個網址有沒有如果沒有那麼它將從本地文件加載 – Navi

+0

@Popeye有什麼辦法通過點擊一個UIWebView – Navi

回答

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

編輯:不要讓這種委託方法得到所要求的第一time.Add一些邏輯,它不會被調用的第一時間加載網頁流量。

SOP的新的要求,請參閱下面的評論

如何來檢測的UIWebView觸摸,

1)tapgesture添加到您的WebView。(在此之前,在你的ViewController添加UIGestureRecognizerDelegate。 1H)

例如:

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事件等等....希望這有助於你..

+0

有沒有什麼methdoe知道webview被點擊或不? – Navi

+0

是的..添加了tapgesture ... –

+0

iOS編碼器ios,一個好方法或壞? – Navi

相關問題