Wednesday, May 1, 2013

NSURL initWithString example ios


initWithString:

Initializes an NSURL object with a provided string.
- (id)initWithString:(NSString *)URLString
Parameters of [NSURL initWithString]
URLString
The string with which to initialize the NSURL object. This string must conform to URL format as described in RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
Return Value
An NSURL object initialized with URLString. If the string was malformed, returns nil.
Discussion of [NSURL initWithString]
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
Example of [NSURL initWithString]
NSString *webStr = [[NSString alloc] initWithFormat:@"%@",[webArray objectAtIndex:1]];

NSLog(@"urlString = %@",webStr); // its printing correct url string

NSURL *webURL = [[NSURL alloc] initWithString:[webStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

NSLog(@"url = %@",webURL); // it should print it

[webURL release];

[webStr release];
Example of [NSURL initWithString]
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];