【问题标题】:iOS - handling redirects in UIWebViewiOS - 在 UIWebView 中处理重定向
【发布时间】:2013-03-11 19:57:29
【问题描述】:

我在 web 视图中有这个 bool 函数,它检查关键字“youtube”,如果链接中有关键字“youtube”,它将在应用程序 web 视图而不是 safari 中打开。这工作正常,但有一个我遇到的问题。如果 youtube 链接被 https://bitly.com/ 缩短,它将在 safari 中打开。有没有办法防止这种情况。我仍然需要 https://bitly.com/ 在 Safari 中为除 youtube 之外的所有其他关键字打开。

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

【问题讨论】:

  • 我知道的唯一方法是手动检查重定向链接(例如 NSURLConnection)。

标签: ios objective-c uiwebview


【解决方案1】:

您可以使用knowurl 服务从微小的缩短链接中扩展原始 URL

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

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2014-09-21
    • 1970-01-01
    • 2012-02-12
    • 2020-04-09
    • 1970-01-01
    • 2015-06-10
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多