Jekil Patel
Jekil Patel

Reputation: 39

NSURL object value is null

I am generating a URL as given by string parameters but url gives null value. For generating URL I have implemented the following code .

 NSURL *url = [[NSURL alloc] init];
 NSString *strURL = @"ftp://Administrat:ABC(R%-@[email protected]/arrows.png";
 url = [NSURL URLWithString:[NSString stringWithString:strURL]];
 NSLog(@"URL :: %@",url);

Thanks

Upvotes: 0

Views: 847

Answers (3)

Mike Abdullah
Mike Abdullah

Reputation: 15003

From your posted code sample, here is the problem:

 NSString *strURL = @"ftp://Administrat:ABC(R%-@[email protected]/arrows.png";

Look carefully at the authority component (username, password and host). An @ symbol is used in URLs to separate username & password from the host. Because your password contains an @ character, it must be percent escaped. The percent encoding of @ is %40, giving you instead the code:

 NSString *strURL = @"ftp://Administrat:ABC(R%-%[email protected]/arrows.png";

You really ought to be escaping other URL-specific characters in there too, like the lone % symbol.

Upvotes: 0

Vinayak Kini
Vinayak Kini

Reputation: 2919

Make use of the following code.

NSString *sURL = @"ftp://www.jerox.com/Administrator:@123@TRDOP@%$/arrows.png";
NSURL *url = [[NSURL alloc] initWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"URL :: %@",url);

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107121

You need to escape the special characters in the url query string.

Use:

NSString *strURL = @"ftp://www.jerox.com/Administrator:@123@TRDOP@%$/arrows.png";
strURL =[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(@"URL :: %@",url);

Also I need to mention some mistakes in your code:

  • No need to allocate and init the NSURL object, because you are again assigning another object to that pointer
  • No need of using stringWithString: there

Upvotes: 2

Related Questions