NSURLConnection的错误代码"-1009"是什么意思?

jxx*_*jxx 32 macos cocoa cocoa-touch nsurlconnection ios

当我发送请求并收到错误代码错误时-1009,这意味着什么?我不知道如何处理它.

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

   if(error.code==-1009){       
      //do something           
   }    
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*ust 66

由于返回的错误应该在NSURLErrorDomain,所以代码-1009意味着:

NSURLErrorNotConnectedToInternet

请求网络资源时返回,但未建立Internet连接,无法通过缺少连接或用户选择不自动建立网络连接.


ric*_*ira 12

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

switch NSURLError(rawValue: error.code) {
case .Some(.NotConnectedToInternet):
    print("NotConnectedToInternet")
default: break
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

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

斯威夫特4:

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


Mor*_*ast 5

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

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