36

I have problem with NSURL. I am trying to create NSURL with string

code

    NSString *prefix = (@"tel://1234567890 ext. 101");
    NSString *dialThis = [NSString stringWithFormat:@"%@", prefix];
    NSURL *url = [[NSURL alloc] initWithString:dialThis];
    NSLog(@"%@",url);

also tried

    NSURL *url = [NSURL URLWithString:dialThis];

but it gives null . what is wrong ?

Thanks..

1
  • Im not sure, but you can try to NSString *prefix = (@"tel://1234567890 ext. 101"); change to NSString *prefix = @"tel://1234567890 ext. 101"; Commented Apr 22, 2011 at 9:41

7 Answers 7

61

Your problem is the unescaped spaces in the URL. This, for instance, works:

    NSURL *url = [NSURL URLWithString:@"tel://1234567890x101"];

Edit: As does this..

    NSURL *url2 = [NSURL URLWithString:[@"tel://1234567890 ext. 101"
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Sign up to request clarification or add additional context in comments.

3 Comments

but actual string comes from XML parsing say(123-456-7891 ext. 101) and I want call phone call function.
i used your code it gives : tel://1234567890%20ext.%20101 as output !!! " % " symbol ???
That's because %20 means a space character in escaped URI.
3

Before passing any string as URL you don't control, you have to encode the whitespace:

 NSString *dialThis = [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 // tel://1234567890%20ext.%20101

As a side note, iOS is not going to dial any extension. The user will have to do that manually.

From Apple URL Scheme Reference: Phone Links:

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.

Comments

1

Im not sure the "ext." in phone number can be replce by what value? but you can try like this,

NSString *prefix = [NSString stringWithString: @"tel://1234567890 ext. 101"];

    NSString *dialThis = [NSString stringWithFormat:@"%@", prefix];

    NSURL *url = [NSURL URLWithString:[dialThis stringByReplacingOccurrencesOfString:@" ext. " withString:@"#"]];
// it might also represent by the pause symbol ','.

you can go to find the ext. is equivalent to what symbol in the phone, then replace it.

but dunno it can be work in actual situation or not....

4 Comments

right now I just want to print the url as tel://1234567890 ext. 101
like Jim Blackler has stated above, NSURL not support for 'space' directly, therefore when it come to normal web url, it must be encode with %20, but since now it is a phone link, so the 'ext.' in normal case must represent by a symbol, but Black Frog has point out Apple not allow for any '#', but no harm to try it.
hmm ok .... one more thing can I start phone call function in simulator ? i tried with some static url but nothing is happening !
simulator cannot start the phone call, must in phone.
1

As with iOS 9.0,

stringByAddingPercentEscapesUsingEncoding:

has been deprecated.

Use the following method for converting String to NSURL.

let URL = "URL GOES HERE"
let urlString = URL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())

Comments

1

If you've got something you think should be a URL string but know nothing about how URL strings are supposed to be constructed, you can use NSURL's URLWithDataRepresentation:relativeToURL: method. It parses the URL string (as bytes in an NSData) and percent-encodes characters as needed. Use the NSUTF8StringEncoding for best results when converting your NSString to NSData.

NSURL *url = [NSURL URLWithDataRepresentation:[@"tel:1234567890 ext. 101" dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
NSLog(@"%@",url);

creates a URL with the string 1234567890%20ext.%20101

It attempts to do the right thing. However, for best results you should find the specification for the URL scheme you using and follow it's syntax to create your URL string. For the tel scheme, that is https://www.rfc-editor.org/rfc/rfc3966.

P.S. You had "tel://" instead of "tel:" which is incorrect for a tel URL.

Comments

0

Try this one, It works for me....

NSString *prefix = (@"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:@"%@", prefix];
NSURL *url = [NSURL URLWithString:[queryString stringByReplacingOccurrencesOfString:@" " withString:@"%20"]];
NSLog(@"%@",url);

Comments

0

Make an extension for use in any part of the project as well:

extension String {
    var asNSURL: NSURL! {
        return NSURL(string: self)
    }
}

From now you can use

let myString = "http://www.example.com".asNSURL

or

myString.asNSURL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.