Calling a number containing '#' with URLWithString - ios

I want to programmatically dial a number like #123#.
but [NSURL urlWithString:#"tel://#123#"] returns nil.
I cannot use string encoding because it will make the string change to #"tel://%23123%23" which couldn't be opened by [UIApplication sharedApplication].
Is there any way to solve this problem? Thanks!

If a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.

Related

Tel URI containing * and # is not working on iOS 15 and onwards

Tel URIs containing * and # are not working on iOS 15 and onwards. When I try to execute the following statement
[UIApplication.sharedApplication openURL:[NSURL URLWithString:#"tel://*21*12345#"] options:#{} completionHandler:nil];
It didn't show anything on the device.
NSString *numberToDial = #"*21*12345#";
NSString *encodedNumberToDial = [numberToDial stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", encodedNumberToDial]];
I also try to encode it but it didn't work.
According to the docs: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html
To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone app supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number. If your app receives URL strings from the user or an unknown source, you should also make sure that any special characters that might not be appropriate in a URL are escaped properly. For native apps, use the stringByAddingPercentEscapesUsingEncoding: method of NSString to escape characters, which returns a properly escaped version of your original string.
It appears that this is a limitation of the API and by design because of security.

Using '#' end of openURL

let x = dpadtxt.text! + "#"
let url = URL(string: ("tel://23456712561,2#,\(x)"))!
UIApplication.shared.openURL(url)
when I add # at the end of the string it gives me this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Take a look at Apple URL Scheme reference documentation where you can read about how to work with telephone number encoding.
In that document you can read...
To prevent users from maliciously redirecting phone calls or changing
the behavior of a phone or account, the Phone app supports most, but
not all, of the special characters in the tel scheme. Specifically, if
a URL contains the * or # characters, the Phone app does not attempt
to dial the corresponding phone number. If your app receives URL
strings from the user or an unknown source, you should also make sure
that any special characters that might not be appropriate in a URL are
escaped properly. For native apps, use the
stringByAddingPercentEscapesUsingEncoding: method of NSString to
escape characters, which returns a properly escaped version of your
original string.
You have to encode you number String with something like this
let phone: NSString = "tel://23456712561,2#,#"
if let phone = phone.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
{
print(phone)
}
Please, check if my NSCharacterSet is correct, cause I can't test it on a device right now.

Get request length limit in iOS

I am using Omniture SiteCatalyst in my iPhone app.It uses get request to hit the servers internally via its sdk.However i am facing an issue where some of the request are not reaching the Omniture servers.The get request which is being sent is of variable length depending on the type of request(around 900 + characters).
My question is whether there any limit for the get request length in an iOS app? and if yes
how it would behave in case the request crosses the limit?
Theoretically if URL conforms to RFC 2396 it is fine. According to documentation
The NSURL class fails to create a new NSURL object if the path being
passed is not well-formed; the path must comply with RFC 2396.
Examples of cases that will not succeed are strings containing space
characters and high-bit characters. Should creating an NSURL object
fail, the creation methods return nil, which you must be prepared to
handle. If you are creating NSURL objects using file system paths, you
should use fileURLWithPath: or initFileURLWithPath:, which handle the
subtle differences between URL paths and file system paths. If you
wish to be tolerant of malformed path strings, you’ll need to use
functions provided by the Core Foundation framework to clean up the
strings.
But some time there is issue with specail character e.g. space, accents and others. You must [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
It is also possible server could not handle very long urls, if there are any limitation on server, server will simple truncate the rest of url string, if this is an issue then server will return 414 error url too long.

%2C URL causing iOS app crash

I have an iOS application which downloads a JSON feed from this URL:
https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#
I am storing the URL in a NSString for later use. I am also adding a NSString to the end of the URL which contains an access token which I am using for OAuth Authentication (hence the %# at the very end of the URL).
Here is how I am storing the URL:
NSString *pre_yt_user_url = [NSString stringWithFormat:#"https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#", token_youtube];
As you can see part of the URL has a %2C
This is causing a warning and making my iOS app to crash!!
Here are the warning I get:
Format specifies type 'unsigned-short' but the argument has type NSString
and:
More % conversions than data arguments
What am I doing wrong here? Can't I store a URL in a string??
Thanks, Dan.
When using stringWithFormat the % character is the start of a data argument unless it's escaped. So you need to escape it because you don't want to use it as a supplied parameter. You need to use %%2C (because the first % escapes the second %).

Getting NSData from a url works in the simulator but not on a device

I implement a method that send notes to the server:
-(IBAction)inserttotextfied:(id)sender{
NSString *strurl=[NSString stringWithFormat:#"http://localhost/get-data/insert.php?Name=%#&message=%#",txtf.text,txt2.text];
NSData *dataurl=[NSData dataWithContentsOfURL:[NSURL URLWithString:strurl]];
NSString *stresult=[[[NSString alloc]initWithData:dataurl encoding:NSUTF8StringEncoding]autorelease];
NSLog(#"%a",stresult);
}
The problem is when I test it via simulator the is being sent, but when I test it in the device the data did not being saved
You probably don't want to send something to localhost on your device, or do you use a different url on the device build?
Simulator is faster in response. So the url return data and print properly. But on device the response time is higher then simulator. Your NSLog(#"%a",stresult); statement is executing before it get any data from the response. I will suggest to give some delay or use delegate so that you can use data after getting the response.
Format Specifier %a is 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent.
if that is not your intent, try this:
NSLog(#"%#", stresult);

Resources