Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL returns Nil Value

Here Below is my code

NSString *string    = [NSString stringWithFormat:@" http://abc.com  /Demo/View.php?drinkId=%@&name=%@&comment=%@&date=%@&rating=%@&    ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];

NSURL *url          = [[NSURL alloc] initWithString:string];

Here in string there is value but url returns nil. Can Anyone tell why this happened.

Thanks ....

"This won't work, so here's what I did instead"

NSString *string    = [NSString stringWithFormat:@"http://abc.com/Demo/View.php?drinkId=%@&name=%@&comment=%@&date=%@&rating=%@&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];

NSURL *url          = [[NSURL alloc] initWithString:string];
like image 309
Vijay Avatar asked Aug 31 '11 14:08

Vijay


1 Answers

NSURL will return nil for URLs that contain illegal chars, like spaces.

Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].

Here is the class reference for NSString.

like image 137
Mike Hay Avatar answered Oct 17 '22 23:10

Mike Hay