Reputation: 4345
I have a view that, when clicked, should prompt the user to make a phone call. I am attempting to use the code below:
NSString *phoneNumber = [@"tel://" stringByAppendingString:locPhone];
NSString *phoneString = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneString= [phoneString stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneString= [phoneString stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneString= [phoneString stringByReplacingOccurrencesOfString:@"-" withString:@""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneString]];
I know that the code is reached, and that the ultimate value of "phoneString" is a 10 digit phone number. There is no error, but nothing happens. What am I missing?
EDIT: I see now that canOpenURL returns NO. Is this because I'm running this on the simulator?
Upvotes: 3
Views: 620
Reputation: 32681
Your code seems correct, but note that the simulator cannot simulate phone calls (testing it with canOpenURL
on the simulator will return NO
).
You should test your code on an actual device, you will probably see that it works as expected.
By the way, you really should add the canOpenURL
test in your application, and display some alert to the user in that case, because users may run your application on an iPod device for example or on iPad, and neither can perform phone calls.
Upvotes: 2