Let's assume I have the string:
"I love visiting http://www.google.com"
How can I detect the token, http://www.google.com?
You can use NSDataDetectors
These were added in iOS4 and are quite useful. You want to create a data detector with the NSTextCheckingTypeLink
and let it do its thing.
NSString *testString = @"Hello http://google.com world";
NSDataDetector *detect = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [detect matchesInString:testString options:0 range:NSMakeRange(0, [testString length])];
NSLog(@"%@", matches);
You could do something like:
-(BOOL)textIsUrl:(NSString*)someString {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES ^[-a-zA-Z0-9@:%_\\+.~#?&//=]{2,256}\\.[a-z]{2,4}\\b(\\/[-a-zA-Z0-9@:%_\\+.~#?&//=]*)?$"];
[predicate evaluateWithObject:someString];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With