There are many answers for this question, but on iOS 9 there are some problems. When I use this code:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:self.lblPhone.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
I got this error:
LaunchServices: ERROR: There is no registered handler for URL scheme (null)
Then I change the Info.plist with this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
But nothing changed!
So I tried second code and this works:
NSString *numberString = #"004986632461";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#",numberString]]];
But this one not:
NSString *numberString = [NSString stringWithFormat:#"%f", [[_lblPhone text] floatValue]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#",numberString]]];
It's dialing, but the number is wrong. Any idea?
Your second bit of code doesn't work because you are attempting to format the text as a floating point value.
This line:
NSString *numberString = [NSString stringWithFormat:#"%f", [[_lblPhone text] floatValue]];
Will result in a value of 4986632704.000000 for the string #"004986632461".
Skip the float part. Just do:
NSString *numberString = _lblPhone.text;
Don't ever try to treat a phone number as a numeric value. It's text and should only be treated (and stored) as text.
Try:
NSString *phoneNumber = [#"tel://" stringByAppendingString:self.lblPhone.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Previously telprompt was used by most developers since it will return to app after the call while tel will return to home screen. But from iOS 8 onwards, tel will return to the app after call. So you can use that.
I solved this problem myself. It's very easy. I must clean invisible spaces!
NSString *numberString = [_lblPhone.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Related
Im trying to create an application which will open apple maps located in iOS device with given source and destination address.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",[_fromTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]],[_toTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
addr=[addr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
But canOpenURL is not working ! It always returns NO.
I have added
"LSApplicationQueriesSchemes
urlscheme
urlscheme2
urlscheme3
urlscheme4
"
in Info.plist file.
Try below coding.it works perfectly.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",#"Lacock" ,#"Avebury"];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
If error is
"This app is not allowed to query for scheme whatsapp"
1) Check Info plist.
add LSApplicationQueriesSchemes Array
add whatsapp String.
If error is invalid url.
2) Check, whether the string you are passing is not having special characters. Specially whitespace.
string = [string stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
then use this string for url.
Both of these should solve. Cheers!
I'm running iOS 9b5.
In my app, if a device can make a phone call, I want to color the text blue so it looks tappable. If not, I leave it black.
In order to determine the device capabilities, I use:
[[UIApplcation sharedApplication] canOpenURL:#"telprompt://5555555555"]
As we all know, iOS 9 requires we whitelist any URL schemes we'll be using in our app as a privacy measure.
I have this in my Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
</array>
No matter what I do, I still get canOpenURL: failed for URL: "telprompt://" - error: "(null)". I've tried tel:// and sms:// and I can't seem to avoid that syslog warning.
Does anybody know of a way to detect whether or not a device can make a phone call wtihout triggering these warnings?
What I discovered so far is, that if the console logs -canOpenURL: failed for URL: "xxx://" - error: "(null)", it actually works. As soon as there is any other error than null, it may not work. If the error is "This app is not allowed to query for scheme xxx", then you have to add this scheme to your app's .plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>xxx</string>
</array>
Strange behavior that the console output looks like an error although there is none, indeed.
I think you might need to try this on an actual device, or just try it again. I just got this working on my iPhone 5, it looks like you don't even need to add it to the LSApplicationQueriesSchemes. If the app is built with Xcode 7 Beta 6 and you use canOpenURL or openURL like below it seems to work just fine on device.
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:555-555-5555"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:555-555-5555"]]
On the iOS sim I still get the error:
LaunchServices: ERROR: There is no registered handler for URL scheme tel
-canOpenURL: failed for URL: "tel:555-555-5555" - error: "This app is not allowed to query for scheme tel"
I got the same error in IOS9 devices. So I have used below code snippet to avoid this error.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
As iOS9 deprecates stringByAddingPercentEscapesUsingEncoding, the following can be used to clean the telprompt: URL.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
//NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
In iOS9 I'm using this code and it works:
NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:#"AssistanceCallMISDN"];
assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneUrl = [NSURL URLWithString:[#"telprompt://" stringByAppendingString:assistanceNumber]];
NSURL *phoneFallbackUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:assistanceNumber]];
if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) {
[UIApplication.sharedApplication openURL:phoneUrl];
} else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) {
[UIApplication.sharedApplication openURL:phoneFallbackUrl];
} else
{
[[[UIAlertView alloc] initWithTitle:#"" message:[NSString stringWithFormat:#"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %#", assistanceNumber] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
[_viewEmergency setHidden:YES];
}
My Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
Try running this on a real device instead of simulator. No need to add LSApplicationQueriesSchemes for the tel scheme.
try this one:
NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phone_number]]];
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];
I create a UITextView and set text=#"中国,浙江省杭州市滨江区",
set dataDetectorTypes=UIDataDetectorTypeAddress,
then,long pressed, choose open map, it can found the address in GoogleMap.
But, the same address, i used openUrl can't find the address.
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
anybody who can tell me why? or iOS not use this url(http://maps.google.com/maps?q=%#)
Would you try with a different enconding? eg., NSUnicodeStringEncoding
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding]];
And what is the result of
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
?
Google Maps http:// calls don't use % seperators but rather +'s.
NSString *fixedAddress = [fullAddress stringByReplacingOccurencesOfString:#" " withString:#"+"];
NSString *googleCall = #"http://maps.google.com/maps?q=";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[googleCall stringByAppendingString:fixedAddress]]];
I, myself encountered this issue and fixed it with the preceeding code last night.
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"];