Making a phone call in an iOS application - ios

I have some code which attempts to make a call within an application, but it doesn't seem to be working:
UIApplication *myApp = [UIApplication sharedApplication];
NSString *theCall = [NSString stringWithFormat:#"tel://%#",phone];
NSLog(#"making call with %#",theCall);
[myApp openURL:[NSURL URLWithString:theCall]];
Sometimes, the variable phone is something such as #"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?

Yup. You need to take those out yourself. Or you can use the snippet below...
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", cleanedString]];
Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.

To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
just an update on above answer.

Here's a simple method that can be used to make a call and return to the app after the call is finished.
Add the following to your .m file
- (void) dialNumber:(NSString*) number{
number = [#"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
Then add the following code wherever you want to make the call from:
[self dialNumber:#"5031234567"];

Related

Call from ios app with Pause

I am working with VOIP application. I am trying to make call from my ios app which contains Pauses indicated with (,).
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:finalNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
On dialling number, call is not connected. What I can use to allow pause in my number.
Try using tel:// scheme:
Objective-C
NSString *telUrl = [NSString stringWithFormat:#"tel://%#,%#", contactPhone, contactExtension];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telUrl]];
Swift
let formattedPhone = "tel://\(contactPhone),\(contactExtension)"
if let phoneURL = NSURL(string:formattedPhone) {
print(phoneURL)
UIApplication.sharedApplication().openURL(phoneURL)
}
And make sure your string doesn't have white spaces.
Cheers.

Make a phone call from UIWebView without prompt

SCENARIO
I have an app that is a UIWebView, I make some url overriding for requirements.
PROBLEM
To make a call opening url with tel: works weird in iOS7 and iOS8, it makes the phone call direct in the background, but it also ask for the confirmation, so user experience is horrible:
[[UIApplication sharedApplication] openURL:request.URL];
SOLUTION
To solve this issue, I used telprompt. It works nice in all iOS versions:
NSURL *url = [NSURL URLWithString:#"telprompt://637****"];
return [[UIApplication sharedApplication] openURL:url];
But shows this confirmation dialog:
QUESTION
Now, I have a new requirement, to make the phone call without confirmation or prompt. So... There is some way to make a phone call in iOS omitting the confirmation prompt?
I want something like
NSURL *url = [NSURL URLWithString:#"telnoprompt://637******"];
return [[UIApplication sharedApplication] openURL:url];
NSMutableCharacterSet *characterSet =[NSMutableCharacterSet characterSetWithCharactersInString:#" "];
NSArray *arrayOfComponents = [phone_number componentsSeparatedByCharactersInSet:characterSet];
phone_number = [arrayOfComponents componentsJoinedByString:#""];
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phone_number];
NSString *escapedUrlString = [phoneURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneURL = [NSURL URLWithString:escapedUrlString];

telprompt without alert

My app need to dial a number and I know that telprompt return back to app after dial. But it will alert user every time want to call a number, is there any way to direct call number without show a alert view?
you can make a call using bellow code:-
NSString *value =#"9999999999";//your telnumber
NSURL *url = [[ NSURL alloc ] initWithString:[NSString stringWithFormat:#"tel://%#",value]];
NSLog(#"currunt number%#",url);
[[UIApplication sharedApplication] openURL:url];
hope this will help little more
tel directly call the given number without confirmation and did not return to application after call finishes so instead of it , you may use telprompt.
PhoneNumber=#"999999999";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", PhoneNumber]]];

How to make a phone call AND return to the original app afterwards

If I use UIWebView to display a phone number and set the data detector type to UIDataDetectorTypePhoneNumber, then when I click on the number its possible to make a phone call.
After the phone call has ended I am returned back to my app.
However if I attempt to programatically invoke the phone app using
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:123456789"]];
or
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://123456789"]];
Then it is not possible to return back to my app after the call has finished.
Is it possible to be able to programmatically launch the phone app and then return back to my app after the call is finished, the same as if the user clicks on a number in a UIWebView?
Instead of:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://123456789"]];
use:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt://123456789"]];
telprompt:// vs. tel://
This will pop up an alert view and the user will have to tap "call" but this returns to the app after the call is done.
Unfortunately, Apple does not allow this to occur, as when you do the openURL command, it will open that application, and since you cannot access anything within that application, you will have to re-enter your application yourself. You can however save your state in NSUserDefaults, and then have the user be able to go back to the same part of the app as when they left. Check here for help: iOS Human Interface Guidelines
use for instance
NSString * telNummer;
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([osVersion floatValue] >= 5.0) {
telNummer = [NSString stringWithFormat: #"telprompt://%#", person.telephoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telNummer]];
} else {
telNummer = [NSString stringWithFormat: #"tel://%#", person.telephoneNumber];
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:telNummer]]];
webview.hidden = YES;
// Assume we are in a view controller and have access to self.view
[self.view addSubview:webview];
[webview release];
}
you can send local notification 2-3 seconds after the dial app is visible to the user.
it's not perfect, but clicking on the local notification is and getting back to the original app, its easier the double click on the home button and switch app.
NSURL *url = [NSURL URLWithString:#"telprompt://your phone number"];
[[UIApplication sharedApplication] openURL:url];
This might work?
UIWebView webView = [[UIWebView alloc] init];
NSURL *url = [NSURL URLWithString:#"tel:123456789"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

How can I make phone call in iOS?

How can I make a phone call in Objective-C?
You can initiate a call
https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html
So this would probably work:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:12125551212"] options:#{} completionHandler:nil];
This is clipped from a project I did to do just that:
NSString *phoneStr = [NSString stringWithFormat:#"tel:%#",phone_number];
NSURL *phoneURL = [NSURL URLWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
It may also be helpful to know how to prompt the user to call a number:
NSURL *phoneNumber = [NSURL URLWithString:#"telprompt://13232222222"];
[[UIApplication sharedApplication] openURL:phoneNumber];
telprompt gives the user a choice to place the call or cancel making the call before the phone dials. The two forward slashes after the colon are optional.
well if you are talking about using objective-c to make a phone call on the iphone, then you can do something like this:
NSURL *phoneNumber = [[NSURL alloc] initWithString: #"tel:867-5309"];
[[UIApplication sharedApplication] openURL: phoneNumber];
If you are talking about doing this on a mac ,well, then like others have mentioned that is specific based on number of things like, if you are using voip, a modem, connecting through something like an Asterisks box, etc..
openURL is deprecated.
Now use this:
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString: #"tel:12125551212"] options:#{} completionHandler:nil];
REMOVE EMPTY SPACES IN PHONE NUMBER
NSString *phoneNumberString = #"123 456";
phoneNumberString = [phoneNumberString stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumberString = [NSString stringWithFormat#"tel:%#", phoneNumberString];
NSURL *phoneNumberURL = [NSURL URLWithString:phoneNumberString]];
[[UIApplication sharedApplication] openURL:phoneNumberURL];
NSString *phoneNumber = #"Phone number here";
UIWebView *webView = [[UIWebView alloc] init];
NSURL *url = [NSURL URLWithString:numberString];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
webView.dataDetectorTypes = UIDataDetectorTypeNone;
[webView loadRequest:requestURL];
This will either be very platform-specific, or you'll have to use a wrapper library to account for the differences among platforms, so you better state what platform this is intended for. In general, there are various telephony APIs available on most platforms.
On Windows systems there's for example the "TAPI", also things may somewhat differ if you are targeting a digital telephone system such as ISDN, because there are other APIs available.

Resources