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

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.

Related

Dialing a phone number in Xcode [duplicate]

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>"]];

Navigate to settings screen in iphone

I want to move from my app to settings screen of iphone when a button in the app is pressed. Can any one please help by telling how to do it? Any help is appreciated. Sorry for my bad english.
Thanks
prefs:root=General pattern no longer works for iOS Latest versions !!!
There is an update in it. The following works fine in iOS 8 ..
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=General"]];
you may look at this
iOS Launching Settings -> Restrictions URL Scheme
Settings screen can be opened for versions less than IOS 5.0 as it has been disabled for latest IOS's. For Lower versions you can try this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs://"]];
MoreOver you should create your own view and present the editable settings to the user and get the input from him(for the first time or as a menu) and store it using NSUserdefaults.
You can't ask user to enter settings such as server-address port etc and try it each time, if it works.
I have found a solution for navigating from your application to Settings application which works fine iOS 8 or later versions
1) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
This will navigate to settings only if your app settings are absent
if they are present then the above code will navigate to Settings page of your app
I am using the below string to navigate to iCloud page and it works fine
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"App-Prefs:root=CASTLE"] options:#{} completionHandler:nil]
just by replacing the "CASTLE" with "GENERAL"

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.

Error when registering for Multiple Remote Notification Types

XCode is driving me nuts, when I register for multiple notification types:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge];
or
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
It throws and error not a warning:
error: Semantic Issue: Cannot initialize a parameter of type 'UIRemoteNotificationType' with an rvalue of type 'int'
Even though this is the same line that everyone uses (even Apple in their demos). I'm using the iOS 5 SDK (first one that came out after release of iOS 5) & XCode 4.2. The project is a older project (XCode 3 & iOS 4) that is being upgraded and targeted for iOS 5.
I had this problem too. The trick is if this code is in a .mm file (Objective C++), this error occurs. If you aren't using any C++ features, change the filename extension from .mm to .m and it will compile OK.
Try casting to UIRemoteNotificationType:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
For the record I was never able to solve this problem completely. It is definitely a problem with the developer tools. The line in my UIApplicationDelegate didn't work. I was able to work around this by placing this line in my MainViewController rather than my ApplicationDelegate.
put parentheses around the parameters like
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];

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.