菜鸟教程小白 发表于 2022-12-12 09:33:09

iOS - 在 UIWebView 中处理重定向


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

<pre><code>-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([ rangeOfString:@&#34;youtube&#34;].location==NSNotFound){
      [ openURL:];
      return NO;
    }
}
return YES;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <a href="http://knowurl.com" rel="noreferrer noopener nofollow"><strong>knowurl</strong></a>从微小的缩短链接扩展原始 URL 的服务</p>

<pre><code>- (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 ([ rangeOfString:@&#34;youtube&#34;].location==NSNotFound)
            {
               [ openURL:];
               _returnVal =NO;
            }
         }
      }
      @catch (NSException *exception)
      {
         NSLog(@&#34;%s\n exception: Name- %@ Reason-&gt;%@&#34;, __PRETTY_FUNCTION__,,);
      }
   @finally
   {
       return loadedImages;
   }   
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS - 在 UIWebView 中处理重定向,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15347444/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15347444/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS - 在 UIWebView 中处理重定向