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