I'd like to make a button call a phone number entered by the user inside the text field. I have a code but it doesn't work.
NSString * phoneNumber = [NSString stringWithFormat:#"%#%#", #"tel://", phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Anyone has a similar approach to this? Thanks.
I think it's tel: instead of tel://. See this Apple document. Try giving this a shot:
NSString *pn = [#"tel:" stringByAppendingString:phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pn]];
See my answer to another question for some sample code to handle cases with invalid input.
Basically you do this:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", escapedPhoneNumber]];
Update: I noticed that the string you created shares the some name ("phoneNumber") as the text field from which you try to get the text. You may want to rename either of those two.
Related
There is an iOS 5.0+ app, released in App Store, which displays employees profiles with their phone numbers. The users of the app can tap on the phone number and the number will be dialed on their iPhones.
The problem is that some users with iPhone 4 are reporting that it does NOT dial.
This is the code I am using to dial:
NSString *cleanedString = [[self.member.phone componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlText = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", escapedPhoneNumber]];
[[UIApplication sharedApplication] openURL:urlText];
Any ideas about this?
There shouldn't be any / in the tel: scheme:
NSURL *urlText = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", escapedPhoneNumber]];
In the Apple URL Scheme Reference you can see some example for what is allowed in the tel: scheme.
If you use telpromt instead of tel this will return you to app after your call ends|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt:1234567890"]];
I have the following code:
phoneNumber = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSString *phoneNumberURLString = [[NSString alloc] initWithFormat:#"tel://%#", phoneNumber];
NSURL *phoneURL = [[NSURL alloc] initWithString:phoneNumberURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
However, the issue with this is that, sometimes a certain phone number doesn't get called: for example if phoneNumber is (520) 123-1232, then this doesn't call the phone. Any idea on how to standarize the phone number format such that this method works for all cases?
It is the spaces in the number. A space is not a valid URL character. It must be encoded propery as %20.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to make a call programmatically?
In my App, I have one view in which complete address along with phone number of a company gets displayed. What i want is, when a user touches that phone number, it should make a call..
So, how to do it, when user touches that phone number.
Any answer will be appriciated. Thank you guys.
If you put the text in a UITextView there is a setting for it to automatically detect phone numbers. The OS will then highlight the phone number, and if the user taps on it, it will prompt them if they wish to call that number
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1234567890"]];
}
Then connect TouchUpInside to this and you are good to go. Hope that helps!
-(void)callPhone:(id)sender{
NSURL *url = [NSURL URLWithString: #"tel://848444488"];
NSLog(#"Call %#", url);
[[UIApplication sharedApplication] openURL:url];
}
if the phone is in the form 848 44 44 88 use this code to eliminate spaces:
NSString *phoneWithoutSpaces = [[NSString stringWithFormat:#"tel://%#", #"848 44 44 88"] stringByReplacingOccurrencesOfString:#" " withString:#""];
NSURL *url = [NSURL URLWithString:phoneWithoutSpaces];
Do this
NSString *phoneNumber = [NSString StringWithFormat:#"tel:%#", textField.text];
[[UIApplication SharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
I link the following openMap method to a button. The method does work but there is an error message at
NSLog(urlText);
The message shows format string is not a literal (potentially insecure).
Does anyone know how to eliminate this warning?
-(IBAction)openMap:(id)sender{
NSString* addressText = #"New York";
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", addressText];
NSLog(urlText);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}
Change the NSLog to,
NSLog(#"%#", urlText);
Or, remove the NSLog completely ;-)
I am using this code to run the maps.app from my app.
NSString* urlStr = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:urlStr]];
The issue is that I didnt manage to get the current location.
I want something like this:
poi1.url = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=Current Location&daddr=%f,%f",lat,lon];
Have you tried appending the your label after the position data?
http://maps.google.com/maps?q=37.771008,+-122.41175+(You+can+insert+your+text+here)&iwloc=A&hl=en
Edit: And don't forget to encode your URL correctly!
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];