Using '#' end of openURL - ios

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.

Related

I received a URL from a server with a unicode é in it (\U00e9). How do I create an NSURL with it?

Whenever I try to create an NSURL from it, I always receive nil back. How do I make it work?
Small example:
let str = "https://montr\\U00e9al.ca".stringByRemovingPercentEncoding!
NSURL(string: str)
Which always gives me nil back. Even if I replace the \u00e9 with é it still returns nil.
How should I be doing this?
You should make unicode in braces and add percent for it like this:
let str = "https://www.montr\u{00e9}al.ca".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
First, if you have any control over the server, you should fix the server to provide proper URLs. If you don't have control over it, but can choose a different service, do so. There's no guarantee that improperly encoded URLs will be the only problem coming from that server.
If you can't switch servers, you may need to do something like this:
let percentEncodedURLString = NSURL(dataRepresentation: str.dataUsingEncoding(NSUTF8StringEncoding)!, relativeToURL: nil).relativeString
This is the Swift version of what Apple recommends in the Foundation release notes. I strongly recommend that you read that section to understand the issues with other approaches.

ios issue with stringByAddingPercentEscapesUsingEncoding

In my app I need to send some parameters to the url, when I am trying with the stringByAddingPercentEscapesUsingEncoding it is not converting correctly. If I am not using this encoding I am getting null(Exception) from the nsurl.Here is me code.
http://www.mycompurl.co?message=xyz&id=____ here I am sending the id 1 or 2 or any number.
when I convert this string to url by using stringByAddingPercentEscapesUsingEncoding I got
"http://www.mycompurl.co?message=xyz&id=**%E2%80%8B**1" (when I send 1 as parameter). Then I got the 0 data from the Url.
str = [NSString stringWithFormat:#"%#?message=xyz&id=​%#",Application_URL,bootupdateNew];
str = [str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
url=[NSURL URLWithString:str];
NSError* error = nil;
data1 = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
Thank you In advance
Basics
A URL is composed of several components.
Each component has its own rule how the component's source string must be encoded, so that this component becomes valid within the URL string.
Applying stringByAddingPercentEscapesUsingEncoding: will never always produce a correct URL if the string consists of more than one component (and if we assume, we have an unbounded set of source strings - so that the encoded string actually differs from the source string).
It even won't work always with a string which represents any single component.
In other words, for what's worth, stringByAddingPercentEscapesUsingEncoding: should not be used to try to make a URL out of several components. Even getting the URL query component correctly encoded is at least error prone, and when utilizing stringByAddingPercentEscapesUsingEncoding: it still remains wonky. (You may find correct implementations on SO, though - and I posted one myself).
But now, just forget about it:
It took awhile for Apple to recognize this failure, and invented NSURLComponents. It's available since iOS 7. Take a look! ;)

%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 %).

NSURL not returning nil for one-character non-ascii strings

I have been using NSURL to do a simple URL validation, mostly to weed out non-ascii special characters, which I do not want in my particular application. I take a URL as input into an NSString, then try to create an NSURL using URLWithString. If this returns nil, the app presents an error message.
For example if I enter "あか" as input (that is two Japanese characters), then the NSURL is nil. This has been working as expected. However I recently noticed that entering a string that is contains only one single non-ASCII character, NSURL processes it and returns a URL-encoded value. So if I enter "あ" as input, the resulting NSURL is NOT nil. The absoluteString value is "%E3%81%82".
I'm wondering if this is a bug in NSURL, or some kind of loophole that I'm not understanding.
I'm using Xcode 3.2.5, and the iOS 4.2 SDK.
I can't explain the behavior you are seeing above, however, if all you are trying to do is determine whether a URL string contains any non-ASCII characters, you could achieve this with the following code:
NSString *testURLString = #"http://www.googleあか.com";
NSCharacterSet* ascii = [NSCharacterSet characterSetWithRange: NSMakeRange(0, 128)];
NSCharacterSet* nonAscii = [ascii invertedSet];
if ([testURLString rangeOfCharacterFromSet:nonAscii].location != NSNotFound) {
NSLog(#"This string contains non-ASCII characters");
}

Calling a number containing '#' with URLWithString

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.

Resources