iphone - UIWebView 在 Safari 中打开 URL,但不是以 # 开头的 URL
<p><p>我尝试使用下面的代码在 Safari 中打开我的 HTML5 应用程序中的链接。但是,该代码还在 Safari 中打开了用于应用程序内部导航的 # 链接。链接是否以 HTTP 开头,导致它们在 Safari 中打开?如果是这样,我该如何修改此脚本以排除它们?</p>
<p>谢谢。</p>
<p>供引用</p>
<p>请在此处查看 GIT 存储库:<a href="https://github.com/philhudson91/flaming-cyril" rel="noreferrer noopener nofollow">https://github.com/philhudson91/flaming-cyril</a> </p>
<p>或者我可以编写它来阻止来 self 托管的域的链接打开吗?</p>
<p>更新</p>
<p>这是我现在使用的代码...</p>
<pre><code>-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *requestURL =[ [ request URL ] retain ];
NSCharacterSet * set = [ invertedSet];
if ([ [ requestURL scheme ] isEqualToString: @"http" ]) NSLog(@"HTTP"); if ([ [ requestURL scheme ] isEqualToString: @"https" ]) NSLog(@"HTTPS"); if (( [ rangeOfCharacterFromSet:set].location == NSNotFound )) NSLog(@"Not Local"); if (( [ [ requestURL scheme ] isEqualToString: @"mailto" ])
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
[ requestURL release ];
return YES;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以尝试以下方法:(我没有测试,这只是验证请求方案为http或https时URL不包含'#')</p>
<pre><code>-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *requestURL =[ [ request URL ] retain ];
NSCharacterSet * set = [ invertedSet];
NSLog();
if ( ((( ([ [ requestURL scheme ] isEqualToString: @"http" ]) ||
([ [ requestURL scheme ] isEqualToString: @"https" ])) &&
( [ rangeOfCharacterFromSet:set].location != NSNotFound ) ) ||
( [ [ requestURL scheme ] isEqualToString: @"mailto" ]) ) &&
( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
[ requestURL release ];
return YES;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - UIWebView 在 Safari 中打开 URL,但不是以 # 开头的 URL,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/12372228/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/12372228/
</a>
</p>
页:
[1]