Dialing a phone number in Xcode [duplicate] - iphone

This question already has answers here:
Make a phone call programmatically
(13 answers)
Closed 8 years ago.
Hello I want the user to click on a button and the button will open the actual iPhone phone app.
Any ideas or tutorials on how to do this?

You can easily place a call with this line below:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:12125551212"]];

You can not open just Phone application to manually enter number. As suggested before you can place a call from your application which will open the default phone calling screen and once you are done with that call it will redirect back to your application.

Just to add (without just copying & pasting the same answer yet again !) that if your phone number contains any spaces, then this line of code will silently fail.
So even if the Phone app says that it's dialling +41 44 123 4567, if you attempt to use the following line in your code, nothing will happen. It won't recognise this as a phone number.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:+41 44 123 4567"]];
So, you need to strip out any spaces, then get the Phone app to dial the number, at which point it'll add it's own spaces back into the phone number when it displays it !!
Here's the code I've used (where the phone number is currently displayed in a UILabel called lblPhone):
NSString* phoneNumber = [self.lblPhone.text stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString* actionStr = [NSString stringWithFormat:#"telprompt:%#", phoneNumber];
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:#"iPhone"] ) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionStr]];
}
This code also checks whether you're running the app on an iPad. If you are, then we don't want to attempt to dial the number.
Also, as the answer on this thread suggests, you'll see that I've used telprompt: rather than tel: It has two advantages:
First, it makes a small dialog appear, to check that the user really wants to call that phone number:
And secondly, when the user finishes calling this number (and hangs up), it'll return you back into your app. Strangely, using tel: doesn't do this.
(Tested with XCode 5 & iOS 7.1)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:<You button title as phone number>"]];

Related

Access Settings app in iOS

Is there an opportinity to show up the settings.app in iOS by clicking on a button? It should work with iOS 5.1 so the "prefs:root..." url is no option.
Do you have an idea how to solve this?
I know the question is about 5.1 specifically, but in case anyone else is interested:
As of iOS 8, it is possible to take a user from your app directly into the Settings app. They will be deep linked into your app's specific Settings page, but they can back out into the top level Settings screen.
UPDATE:
Thanks to Pavel's comment, I simplified the if statement and avoided the EXC_BAD_ACCESS on iOS 7.
UPDATE 2:
If your deployment target is set to 8.0 or above, Xcode 6.3 will give you the following warning:
Comparison of address of 'UIApplicationOpenSettingsURLString' not equal to a null pointer is always true
This is because the feature was available starting in 8.0, so this pointer will never be NULL. If your deployment target is 8.0+, just remove the if statement below.
if (&UIApplicationOpenSettingsURLString != NULL) {
NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:appSettings];
}
You are not able to do this on iOS 5.1. Most likely Apple removed that ability intentionally (you'll get "Please enter a valid URL", while Twitter can still call the Settings, though). Please refer to:
How to open preferences/settings with iOS 5.1?.
Apple Disables Home Screen Shortcuts For Settings Toggles In iOS 5.1
On iOS 8 Apple gave us the possibility to go to the App Settings right from our app
you can apply this code:
- (IBAction)openSettings:(id)sender {
BOOL canOpenSettings = (UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
iOS6 shows an option to open the settings app directly from an 'AlertView' (shown automatically) if it detects if you're trying to post to FB or Twitter without having those accounts setup.
I have elaborated this over here
The method openURL: is now deprecated.
iOS 10 +
The correct way to open the settings URL (or any URL for that matter) is as follows:
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL options:#{} completionHandler:^(BOOL success) {
// Do anything here
}];

Opening tel: links from UIWebView

I've searched and searched, but I can't seem to find a fix for this problem.
For some reason, I can not get 'tel:' links to work in a UIWebView. When the links are clicked, the message "The URL can't be shown" appears. Clicking on the same link in Safari works perfectly and dials the number.
This problem started with iOS 5. These links worked perfectly in iOS 4.2 and 4.3.
I'm not sure what other information might be useful, so please let me know if I need to clarify.
Thanks!
EDIT:
Here is the actual code in use...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"tel"]) {
return YES;
}
if (![url.scheme isEqualToString:#"http"] && ![url.scheme isEqualToString:#"https"]) {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO; // Let OS handle this url
}
}
[NSThread detachNewThreadSelector:#selector(startBusy) toTarget:self withObject:nil];
return YES;
}
If I take out the first if statement, the number gets dialed immediately with no confirmation. I'd really like it to function the way it used to by giving an alert, giving you the option to hit either 'Call' or 'Cancel' before dialing the number.
If launching as an HTML link, the tel URL scheme will be opened if they appear as:
1-408-555-5555
If you are launching from a native URL string (meaning you coded this in Objective-C and are not serving it via a WebView), your URL string should look like this:
tel:1-408-555-5555
Note: This only works with iOS devices that have the Phone app installed (that means iPhone only). iPad & iPod Touch devices will display a warning message.
Note 2: Ensure the phone numbers you are passing do not contain spaces or other special characters (such as * and #).
Code Feedback
Based on your code, things are a bit clearer now. You comment about how nothing happens when you leave the first if statement in the shouldStartLoadWithRequest method (where you return YES). This is exactly the behavior you should see because your app is not the Phone app. Only the Phone app can handle the tel: URL scheme. By returning YES, you are telling the OS that your app will handle the phone call, but it cannot. You get the call when that conditional is removed because the next block, which checks if ([[UIApplication sharedApplication] canOpenURL:url]) allows the sharedApplication (which, in this case, is the Phone app) to launch the call.
How Things Work & What You Want
The OS is not going to handle showing the Call/Cancel alert dialog for you. That is up to you. It shows up in Safari because the Safari app's shouldStartLoadWithRequest method undoubtedly responds to the tel: scheme by showing a UIAlertView. Your conditional for if ([url.scheme isEqualToString:#"tel"]) should, when YES, trigger a UIAlertView with a Call and Cancel button. On Call, you will tell the sharedApplication to openURL; on Cancel, you will not issue the call & you will also want to return NO so your app does not attempt to loadWithRequest.
Self-Correcting Edit
To be fair about errors in my own thought process, I'm leaving my responses above.
I believe the Call/Cancel dialog is, in fact, a feature of the OS. Apologies for the inaccuracy.
I'd also erroneously glanced over your code's passing off URL handling to sharedApplication only occurring when the scheme was http or https.
After another look at the code, I wonder if, by any chance you have debug options on in Safari? I believe this prevents the alert from popping up. Also--just to double-check the obvious--you aren't trying this inside the simulator, correct? What happens if you remove the conditional check for http/https and just use the canOpenURL check?
However, aside from the error in my comments on the conditional & dialog itself, you still should not be returning YES. To make a phone call, you should only be able to pull that off by passing it to sharedApplication:openURL and ensuring you return NO because your app is not the Phone app. The only reason you'd want to return YES in this method is if your app is going to handle a tel: link in a special way that doesn't involve sending it to the Phone app.
If you created the UIWebView in a .xib, select the UIWebView and check its attributes in the Attribute Inspector. The first heading should be 'Web View', and under that it provides a list of checkboxes marked 'Detection'. Ensure that 'Phone Numbers' is checked.

Problem with a call button - doesn't open tel:// URL

I am trying to make a call button that will call 18 which is Fire Dept in France.
So my code is :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://18"]];
It doesn't work and I get this message in the console :
< warning > Ignoring unsafe request to open URL tel://18
But I saw other application that have the same button fully working !
I am trying this on the device of course.
What am I missing ?
Ok I Found the answer after wasting many hours...
Here is the solution :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://18?1"]];
The iPhone accepts the interrogation mark but ignores it when initiating the call !
In Safari, if you try this :
tel://18
It won't work but if you try this :
tel://18?1
It will call 18 !
The correct url has no backslashes. That is, it should be tel:18
tel://18?1 works fine on iOS4, but it seems that iOS5 ignores everything after the ? character and refuses to just call 18...
However, tel://18&sharp;1 works on iOS5 (and calls number 18) but not on iOS4, so you have to switch according to the iOS version of the client.

IPHONE: How do I override the "cancel\call popup-box" that comes when dialling a number programmatically?

I'm currently developing a project that at some part calls a number of the user's choosing. I use the snippet below to call the number.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:newNumberString]];
The problem is that the box that pops up has buttons with titles in English, but my program is not in English, so it kinda sucks :(
Is there a way to override the title on the cancel\call buttons or maybe an alternative way of dialing a number from code?
In my application the title/buttons are correctly localized by the system. You should try to set your iPhone to the language you want to use.

problem using tel: URL to initiate a call

I'm trying to initiate a call from within an iPhone app.
This related code works and opens Safari as expected:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.apple.com"]];
But, when I replace the http URL with a tel URL the resulting code does not invoke the phone app:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:3035551212"]];
No exceptions or alerts are generated (in the simulator or on a device).
Any idea what the problem might be with my invocation?
Thanks.
The iphone will dial a number using either of the formats listed below. But, it will do nothing if you are in the simulator. It took me 30 minutes of banging my head to figure this out.
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:1-541-555-1234"]];
Link for Apple documentation on the tel: url scheme
Link for openURL documentation
(and )are actually no problem if you use stringByAddingPercentEscapesUsingEncoding as suggested by samiq. Same goes for +, / and spaces. It's a URL, so escaping seems natural.
If I'm guessing right, Apple's regular phone number recognition will be used in the tel: scheme handler, if you escape correctly.
(missing reputation to make it a comment)
As #bentford said one might get miscarried because the simulator does show an alert when you try to click on a phone on the contacts app, this is just an alert that gets generated because the app checks whether or not the tel: protocol is supported on the device or not.
Adding to what he writes you might want to also add support to escape any special characters or spaces as in:
NSString *phoneStr = [NSString stringWithFormat:#"tel:%#",[self.contactDetails objectForKey:#"phone"]];
NSString *escaped = [phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
Hope it helps.
Cheers.
If your phone number has ( or ) in it, the Phone app will not launch.
Haven't had any problems with it using tel:{phone-number} and invoking it the same way you are. Only works on the iPhone device, though.
One thing I did have to do was strip out extraneous characters (like all parentheses, spaces, dashes and dots) out of the phone string. Some of the examples above (but not the original post) have dashes. That might be the problem.
I found that the issue was due to characters that where not allowed for tel:// URI - the following code solved my problem:
NSMutableCharacterSet *charSet = [NSMutableCharacterSet new];
[charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
NSArray *arrayWithNumbers = [str componentsSeparatedByCharactersInSet:charSet];
NSString *numberStr = [arrayWithNumbers componentsJoinedByString:#""];
I just ran into this when trying to add a "Call" button to a UIAlertView. I had the following code to handle the call:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1-602-555-1212"]];
}
}
It wouldn't open anything, just like you. I even tried a regular http URL. It turned out I had forgotten to set the delegate to self. That's probably your problem also.
some additional info about making phone calls via url scheme (as I think someone may find it useful)
How to use tel: with * (star, asterisk) or # (hash, pound) on iOs?
Here is an update on how to derive a phone number in 2018 (where iOS 11 and 12 are most common):
NSString *phoneNumberURLString = [#"tel://" stringByAppendingString:[result.phoneNumber
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet URLPathAllowedCharacterSet]]];
NSURL *phoneNumberURL = [NSURL URLWithString:phoneNumberURLString];
samiq's answer was what I used initially until I found that my app would crash due to a nil NSURL being created (and later being passed into a collection object) due to failing to properly escape a phone number formatted like so: (###) ###-####. The aforementioned code resolves this.
I got the same problem and at last I found the reason was that there was a space in the phone number (for better formatting). After removing the space it worked fine.
The URL should be tel://3035551212 and not tel:3035551212...
Add that // and it should work.