ios NSURLConnection 的错误代码“-1009”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6689407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:45:10  来源:igfitidea点击:

What does NSURLConnection's error code "-1009" mean?

iosmacoscocoacocoa-touchnsurlconnection

提问by jxx

When I send a request and get an error with the error code -1009, what does that mean? I'm not sure how to handle it.

当我发送请求并收到带有错误代码的错误时-1009,这是什么意思?我不知道如何处理它。

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
      NSLog(@"connection didFailWithError");

   if(error.code==-1009){       
      //do something           
   }    
}

回答by DarkDust

Since the error returned should be within the NSURLErrorDomain, the code -1009means:

由于返回的错误应该在 内NSURLErrorDomain代码的-1009意思是:

NSURLErrorNotConnectedToInternet

Returned when a network resource was requested, but an internet connection is not established and cannot be established automatically, either through a lack of connectivity, or by the user's choice not to make a network connection automatically.

NSURLErrorNotConnectedToInternet

当请求网络资源但未建立 Internet 连接并且无法自动建立时返回,这可能是由于缺乏连接或用户选择不自动建立网络连接。

回答by ricardopereira

With Swift, you can use the NSURLErrorenum for NSURL error domain check:

使用Swift,您可以使用NSURLError枚举进行 NSURL 错误域检查:

switch NSURLError(rawValue: error.code) {
case .Some(.NotConnectedToInternet):
    print("NotConnectedToInternet")
default: break
}

Swift 3:

斯威夫特 3:

switch URLError.Code(rawValue: error.code) {
case .some(.notConnectedToInternet):
    print("NotConnectedToInternet")
default: break
}

Swift 4:

斯威夫特 4:

switch URLError.Code(rawValue: error.code) {
case .notConnectedToInternet:
    print("NotConnectedToInternet")
default: break
}

回答by Morten Fast

It's NSURLErrorNotConnectedToInternetwhich means, well, that you're not connected to the internet... :)

NSURLErrorNotConnectedToInternet意味着,嗯,你没有连接到互联网...... :)

You can find the error codes in NSURLError.h.

您可以在 中找到错误代码NSURLError.h