I’m parsing UIWebViewNavigationTypeLinkClicked to launch Safari, dial a number etc.
Occasionally there will be a named anchor that I want to jump down the UIwebview, but also want to check it’s not a redundant file link left in the HTML.
Solution is to check wether the link starts with a #
I’m using this it checks if # exists not if the links starts with #
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];
}
}
}
Is there a way of only matching the first character?
You can use
if ([[requestURL scheme] substringWithRange:NSRangeMake(0,1)] isEqualTo:@”#”)
Note: untested, let me know if it works.