0

I am trying to create an app that allows you to go to a certain app for the appropriate app (specifically, regular web links go to Safari, YouTube links go to YouTube, Google map links go to the Maps app, lines of text goes to the Notes app, and Phone numbers goes to either Contacts (for any iOS device other than the iPhone) or the Phone app. All of the data is viewed in a Table View.

For now, I have an NSMutableArray that has some dummy data (specifically, one of each type of data). I have been successful with the web link, YouTube link, and Google maps link. The issue is, I don't know how I can get it to recognize national and international phone numbers.

Here is the NSMutableArray data:

NSMutableArray *sites = [[NSMutableArray alloc] initWithObjects:@"http://www.apple.com/", @"http://www.youtube.com/watch?v=QH2-TGUlwu4", @"http://maps.google.com/maps?saddr=CN+Tower&daddr=1+Yorkdale+Road+%23500,+Toronto,+ON+M6A+2T9,+Canada+(Yorkdale+Shopping+Centre)&hl=en&sll=43.68515,-79.422475&sspn=0.133818,0.308647&geocode=FdzumQIdjqBE-yGBdRzexNjtFQ%3BFUk0mwIdOp1D-yG5Hg9iD9hImQ&vpsrc=0&t=w&mra=ls&z=12", @"This is a test", @"(416)-555-5555", nil];

Also...

self.cloudsendList = sites;

Here is the code that I have so far in terms of when the user taps on a table view cell:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"%@", [cloudsendList objectAtIndex:indexPath.row]]]];
}

I don't know if I have to do this with lots of lines of code, but, is it possible? And if it is, how would you be able to accomplish it? If use need to see more code in order to understand, then I can gladly post the relevant information. I hope that I haven't caused too much trouble, but I hope that you could help me. I apologize if this is making sound like a total noob. Thanks in advance! :)

chrisjr
  • 760
  • 3
  • 11
  • 18

3 Answers3

2

My feeling is that since Apple has already solved this in UITextView's data detectors it would probably be best to use their solution. General purpose phone number detector/formatters are very difficult to implement (even Apple's is not perfect) and you would likely spend a lot of time on a very fragile solution.

I would suggest adding a UITextView to the UITableViewCell contentView (sized and formatted correctly) and make sure that the data detection is on.

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • Ok. Well, then how would the code look like in respect to the code that I currently have? Just to let you know, the tableviewcell with the information has to be selected, not the text itself. – chrisjr Mar 27 '12 at 00:12
  • Set the UITextView to editable=NO. The text becomes links like on a webpage and it automatically does the right thing. This is the easiest to get going, but you might not like the visual style. – sosborn Mar 27 '12 at 00:36
  • Hmmm... what about NSPredicate? Would you recommend that? – chrisjr Mar 27 '12 at 11:50
  • If this works give credit to Derrick. Check out the second answer on this question: http://stackoverflow.com/questions/4590440/how-can-i-extract-a-url-from-a-sentence-that-is-in-a-nsstring – sosborn Mar 27 '12 at 12:38
1

Instead of rolling your own, checkout Apple's NSDataDetector class.I'm 99% certain this class uses a bunch of Regex's, but the benefit is that it's withstood much more testing than you could ever reasonably do yourself.

Derrick
  • 2,356
  • 5
  • 32
  • 43
0

You're going to want to use regular expressions. That's the only way to do it proper. Here is an index of many of them.

http://regexlib.com/DisplayPatterns.aspx?categoryId=7&cattabindex=6&AspxAutoDetectCookieSupport=1

I'm not sure that you're going to be able to detect all countries with one regex. You may need to do several passes to sweep for all of them.

So, scrub through all your data, store your matches for that chunk of data, and then invoke the url with that data. Surely you know how to if/else in obj c.

FlavorScape
  • 13,301
  • 12
  • 75
  • 117
  • Ok, so let's say it recognized UK numbers (^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$) and US/Canada numbers only (^(1?(-?\d{3})-?)?(\d{3})(-?\d{4})$), how would the code look like in respect to the code that I currently have. I imagine that I would need to use the "if" statements and I know that I would need to use "tel:". – chrisjr Mar 26 '12 at 23:31
  • Not sure how exactly in objective-c Try this out. I can't really tell from your code what all is happening. http://stackoverflow.com/questions/422138/regular-expressions-in-an-objective-c-cocoa-application – FlavorScape Mar 26 '12 at 23:50