热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS网络请求错误代码解析

在使用iOS应用时,遇到网络请求错误是常见的问题。本文将探讨两种常见的错误代码-1003和-1001,并提供详细的解释和解决方案。

最近在使用某款 iOS 应用时,发现无论怎么刷新都会遇到错误 -1003。尝试切换到 Wi-Fi 网络后,错误代码变为 -1001。经过一番搜索,了解到这两个错误分别表示“找不到主机”和“请求超时”。为了更好地理解这些错误,我们可以参考苹果官方文档中的错误代码定义。


官方链接为:官方文档链接


以下是 Swift 和 Objective-C 中部分错误代码的定义:


Swift 定义


var NSURLErrorUnknown: Int { get }
var NSURLErrorCancelled: Int { get }
var NSURLErrorBadURL: Int { get }
var NSURLErrorTimedOut: Int { get }
var NSURLErrorUnsupportedURL: Int { get }
var NSURLErrorCannotFindHost: Int { get }
var NSURLErrorCannotConnectToHost: Int { get }
var NSURLErrorDataLengthExceedsMaximum: Int { get }
var NSURLErrorNetworkConnectionLost: Int { get }
var NSURLErrorDNSLookupFailed: Int { get }
var NSURLErrorHTTPTooManyRedirects: Int { get }
var NSURLErrorResourceUnavailable: Int { get }
var NSURLErrorNotConnectedToInternet: Int { get }
var NSURLErrorRedirectToNonExistentLocation: Int { get }
var NSURLErrorBadServerResponse: Int { get }
var NSURLErrorUserCancelledAuthentication: Int { get }
var NSURLErrorUserAuthenticationRequired: Int { get }
var NSURLErrorZeroByteResource: Int { get }
var NSURLErrorCannotDecodeRawData: Int { get }
var NSURLErrorCannotDecodeContentData: Int { get }
var NSURLErrorCannotParseResponse: Int { get }
var NSURLErrorInternationalRoamingOff: Int { get }
var NSURLErrorCallIsActive: Int { get }
var NSURLErrorDataNotAllowed: Int { get }
var NSURLErrorRequestBodyStreamExhausted: Int { get }
var NSURLErrorFileDoesNotExist: Int { get }
var NSURLErrorFileIsDirectory: Int { get }
var NSURLErrorNoPermissionsToReadFile: Int { get }
var NSURLErrorSecureConnectionFailed: Int { get }
var NSURLErrorServerCertificateHasBadDate: Int { get }
var NSURLErrorServerCertificateUntrusted: Int { get }
var NSURLErrorServerCertificateHasUnknownRoot: Int { get }
var NSURLErrorServerCertificateNotYetValid: Int { get }
var NSURLErrorClientCertificateRejected: Int { get }
var NSURLErrorClientCertificateRequired: Int { get }
var NSURLErrorCannotLoadFromNetwork: Int { get }
var NSURLErrorCannotCreateFile: Int { get }
var NSURLErrorCannotOpenFile: Int { get }
var NSURLErrorCannotCloseFile: Int { get }
var NSURLErrorCannotWriteToFile: Int { get }
var NSURLErrorCannotRemoveFile: Int { get }
var NSURLErrorCannotMoveFile: Int { get }
var NSURLErrorDownloadDecodingFailedMidStream: Int { get }
var NSURLErrorDownloadDecodingFailedToComplete: Int { get }

Objective-C 定义


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

通过查阅这些错误代码,我们可以更好地理解应用中遇到的问题,并采取相应的解决措施。例如,错误 -1003 表示 DNS 解析失败,可能是由于网络配置问题或服务器域名解析问题导致的;而错误 -1001 则表示请求超时,可能是由于网络延迟过高或服务器响应慢造成的。


推荐阅读
author-avatar
终-极-幻-想
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有