NSCocoaErrorDomain problem.

I am trying to get the url source so that I can parse the HTML to get the info. 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 fine on the simulator, but on the 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} 
+4
source share
1 answer

This usually means that the Internet is turned off on your device. Or perhaps the encoding you are trying to force to load the web page does not match the one that matches the NSUTF8String encoding.

Trying to change the 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); } 
+9
source

Source: https://habr.com/ru/post/1393804/


All Articles