I have an app that has a UITextField that the user types in a phone number and then hits the call button and then this method is called.
- (IBAction)callVendor:(id)sender {
NSString *phoneNumber = [NSString stringWithFormat:@"tel:%f",
self.phoneTextField.text.doubleValue];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:phoneNumber]];
}
When I try create the string with .intValue the phone number is completely off. I'm dialing Texas rather than Kansas!
When I try to use a double I get an even larger number because the NSURL class ignores the decimal I guess. So I dial something like 555-555-5555-000000
And when I use a float I get a rounding error. Something like 555-555-5634-000000 instead of 555-555-5555-000000. I assume this one is due to binary -> base-10 rounding error.
I don't want to truncate it to 10 numbers in case I need to dial internationally or without area code. What are my options here?
The best you can do is pass the phone number as a NSString. Why convert it to a number? it can even contain a + sign. You can trim all unwanted characters by doing:
NSCharacterSet *charactersToRemove = [[NSCharacterSet characterSetWithCharactersInString:@"+0123456789"] invertedSet];
NSString *newPhoneNumber = [[self.phoneTextField.text componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""];
newPhoneNumber = [NSString stringWithFormat:@"tel:%@", newPhoneNumber];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:newPhoneNumber]];