NSCocoaErrorDomain 问题。

发布于 2024-12-31 22:46:11 字数 560 浏览 0 评论 0原文

我正在尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撞了怀 2025-01-07 22:46:11

这通常意味着您的设备上的互联网已关闭。或者您尝试强制加载网页的编码可能与 NSUTF8String 编码不匹配。

尝试将您的代码更改为:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文