NSCocoaErrorDomain 问题。
我正在尝试获取 url 的来源,以便我可以解析 HTML 代码以获取信息。我有以下代码:
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);
此代码在模拟器上完美运行,但在设备上出现以下错误:
err Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x167150 {NSURL=http://www.google.com}
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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这通常意味着您的设备上的互联网已关闭。或者您尝试强制加载网页的编码可能与 NSUTF8String 编码不匹配。
尝试将您的代码更改为:
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: