Saturday, May 18, 2013

NSString initWithContentsOfURL example ios


initWithContentsOfURL: encoding: error:

Returns an NSString object initialized by reading data from a given URL interpreted using a given encoding.
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
Parameters
url
The URL to read.
enc
The encoding of the file at path.
error
If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
Return Value of [NSString initWithContentsOfURL]
An NSString object initialized by reading data from url. The returned object may be different from the original receiver. If the URL can’t be opened or there is an encoding error, returns nil.
NSString initWithContentsOfURL example 
NSString *myURL = [NSString stringWithString:@"http://mywebsite.com/api-call.php"];
NSURL *url = [[NSURL alloc] initWithString:myURL];
NSString *strResult = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",strResult);
NSString initWithContentsOfURL example 
NSError *error = nil;

NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSString initWithContentsOfURL example