Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL with string

Tags:

iphone

nsurl

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

like image 847
Maulik Avatar asked Apr 22 '11 09:04

Maulik


People also ask

What does Nsurl mean?

An NSURL object is composed of two parts—a potentially nil base URL and a string that is resolved relative to the base URL. An NSURL object is considered absolute if its string part is fully resolved without a base; all other URLs are considered relative.

What is the difference between Nsurl and URL?

They are identical in usage, Apple has chosen to drop the NeXTSTEP prefix in support of using foundation with Swift on multiple platforms like iOS, Android, and Linux. from Apple: "The Swift overlay to the Foundation framework provides the URL structure, which bridges to the NSURL class.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


2 Answers

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]]; 
like image 151
Jim Blackler Avatar answered Oct 02 '22 11:10

Jim Blackler


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.

like image 33
Black Frog Avatar answered Oct 02 '22 10:10

Black Frog