2013-03-11 68 views
2

我在web視圖中使用bool函數來檢查關鍵字「youtube」,如果在鏈接中有關鍵字「youtube」,它將在應用程序web視圖中而不是safari中打開。正確,但是我遇到了一個問題。如果youtube鏈接縮短https://bitly.com/它將在safari中打開。有沒有辦法來防止這一點。我仍然需要https://bitly.com/針對除YouTube之外的其他每個關鍵字在Safari中打開。iOS - 處理UIWebView中的重定向

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType { 
if (inType == UIWebViewNavigationTypeLinkClicked) { 
if ([[inRequest.URL absoluteString] rangeOfString:@"youtube"].location==NSNotFound){ 
     [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
     return NO; 
    } 
} 
return YES; 
} 
+1

擴大原始URL我知道只有這樣,才能做到這一點,手動檢查鏈接重定向(例如NSURLConnection的)。 – 2013-03-11 20:00:50

回答

1

您可以使用knowurl服務,從微小的縮短鏈接

- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 
{ 
     @try 
     { 
      BOOL _returnVal = YES; 

      //convert your bitly url to KowUrl if its contains `bit.ly` 

      if (inType == UIWebViewNavigationTypeLinkClicked) 
      { 
       if ([[inRequest.URL absoluteString] rangeOfString:@"youtube"].location==NSNotFound) 
       { 
       [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
       _returnVal = NO; 
       } 
      } 
     } 
     @catch (NSException *exception) 
     { 
     NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]); 
     } 
    @finally 
    { 
     return loadedImages; 
    }  
}