我正在解析UIWebViewNavigationTypeLinkClicked以启动Safari,拨打号码等。
偶尔会有一个命名锚点,我想跳过UIwebview,但也想检查它不是HTML中剩余的冗余文件链接。
解决方案是检查链接是否以#
开始我正在使用它检查链接以#
开头是否存在#NSURL *requestURL = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[requestURL scheme] isEqualToString: @"file" ]) {
NSLog(@"I think it's a file", nil);
NSString *urlString = [requestURL absoluteString];
NSLog(@"Url as string: %@", urlString);
if ([urlString rangeOfString:@"#"].location != NSNotFound) {
// Do nothing, let it be handled by the web view
} else {
UIAlertView *errorView = [[UIAlertView alloc]
initWithTitle: @"Link Error"
message:@"Sorry!, don't seem to be able to open this link"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[errorView show];
[errorView release];
}
}
}
有没有办法只匹配第一个角色?
答案 0 :(得分:0)
你可以使用 if([[requestURL scheme] substringWithRange:NSRangeMake(0,1)] isEqualTo:@“#”)
注意:未经测试,请告知我是否有效。