Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCocoaErrorDomain issue.

I am trying to get the source of a url, so that I can parse through the HTML code to get information. I have the following code:

 NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
 NSError *err = nil;
 NSString *urlContents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
 if(err){
    NSLog(@"err %@",err);
 }
 NSLog(@"urlContents %@", urlContents);

This code works perfectly on the simulator, but on a device I get the following error:

err Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x167150 {NSURL=http://www.google.com}
like image 615
glenn sayers Avatar asked Jan 18 '23 15:01

glenn sayers


1 Answers

That usually means the internet is down on your device. Or perhaps the encoding you're trying to force the web page to be loaded as is not the one that matches NSUTF8String encoding.

Trying changing your code to this:

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSError *err = nil;
NSStringEncoding encoding;
NSString *urlContents = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&err];
if(urlContents)
{
    NSLog(@"urlContents %@", urlContents);
} else {
    // only check or print out err if urlContents is nil
    NSLog(@"err %@",err);
}
like image 130
Michael Dautermann Avatar answered Jan 25 '23 18:01

Michael Dautermann