AFNetWorking 取消网络请求&网络超时

取消网络请求

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLSessionDataTask *task = [manager GET:@"url地址"parameters:@"入参"progress:^(NSProgress *_Nonnull downloadProgress) {

//数据请求进度

} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {

//请求成功


} failure:^(NSURLSessionDataTask *_Nullable task,NSError *_Nonnull error) {

//请求失败

}];

//取消单个网络请求
[task cancel];

//取消所有的网络请求
[manager.operationQueue cancelAllOperations];

网络超时

  [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 10.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];

timeoutInterval 是AFNetworking本身自带的一个设置超时的键。

设置完之后问题又来了,如果我想在超时之后做一些操作,怎么判断什么时候请求超时?我想起来了每次AFN请求失败我都有打印error,于是我就去看看打印信息,如下图:

1641067-4000dcc20247a205.png

错误码

苹果提供相关错误码如下:

 enum
    {
       NSURLErrorUnknown = -1,
       NSURLErrorCancelled = -999,
       NSURLErrorBadURL = -1000,
       NSURLErrorTimedOut = -1001,
       NSURLErrorUnsupportedURL = -1002,
       NSURLErrorCannotFindHost = -1003,
       NSURLErrorCannotConnectToHost = -1004,
       NSURLErrorDataLengthExceedsMaximum = -1103,
       NSURLErrorNetworkConnectionLost = -1005,
       NSURLErrorDNSLookupFailed = -1006,
       NSURLErrorHTTPTooManyRedirects = -1007,
       NSURLErrorResourceUnavailable = -1008,
       NSURLErrorNotConnectedToInternet = -1009,
       NSURLErrorRedirectToNonExistentLocation = -1010,
       NSURLErrorBadServerResponse = -1011,
       NSURLErrorUserCancelledAuthentication = -1012,
       NSURLErrorUserAuthenticationRequired = -1013,
       NSURLErrorZeroByteResource = -1014,
       NSURLErrorCannotDecodeRawData = -1015,
       NSURLErrorCannotDecodeContentData = -1016,
       NSURLErrorCannotParseResponse = -1017,
       NSURLErrorInternationalRoamingOff = -1018,
       NSURLErrorCallIsActive = -1019,
       NSURLErrorDataNotAllowed = -1020,
       NSURLErrorRequestBodyStreamExhausted = -1021,
       NSURLErrorFileDoesNotExist = -1100,
       NSURLErrorFileIsDirectory = -1101,
       NSURLErrorNoPermissionsToReadFile = -1102,
       NSURLErrorSecureConnectionFailed = -1200,
       NSURLErrorServerCertificateHasBadDate = -1201,
       NSURLErrorServerCertificateUntrusted = -1202,
       NSURLErrorServerCertificateHasUnknownRoot = -1203,
       NSURLErrorServerCertificateNotYetValid = -1204,
       NSURLErrorClientCertificateRejected = -1205,
       NSURLErrorClientCertificateRequired = -1206,
       NSURLErrorCannotLoadFromNetwork = -2000,
       NSURLErrorCannotCreateFile = -3000,
       NSURLErrorCannotOpenFile = -3001,
       NSURLErrorCannotCloseFile = -3002,
       NSURLErrorCannotWriteToFile = -3003,
       NSURLErrorCannotRemoveFile = -3004,
       NSURLErrorCannotMoveFile = -3005,
       NSURLErrorDownloadDecodingFailedMidStream = -3006,
       NSURLErrorDownloadDecodingFailedToComplete = -3007
    }

你可能感兴趣的:(AFNetWorking 取消网络请求&网络超时)