使用 StoreKit 编写未记录的 NSURLErrorDomain 错误代码(-1001、 -1003和 -1004)

我正在编写与 StoreKit 相关的代码,当我试图向队列添加购买时,会得到一些相当麻烦的错误代码。

到目前为止,我已经经历了错误代码 -1003和 -1004,我不能找到任何关于这些代码在互联网上。

运行产品请求会返回有效的产品编号,所以我不知道为什么对 [[SKPaymentQueue defaultQueue] addPayment:aPayment];的调用会因为这个未记录的问题而失败。

同样的代码在一个设备上也可以正常工作,但是在另一个设备上不会出现错误。

到目前为止,我的问题还没有得到回答:

这些密码是什么意思? 我如何减轻这个问题? 为什么他们发生在购买尝试,而不是产品请求?

我所做的故障排除包括重新生成签名证书和供应配置文件、更改 WiFi 网络、清理和构建以及重新安装所有相关的软件和组件,这些事情没有一件单独或一起帮助解决这个问题。

编辑:

在苹果开发论坛上发现了一个关于这个的讨论,但是没有苹果的人回复: https://devforums.apple.com/thread/107121?tstart=75(需要查看 iOS 开发者帐户)

编辑:

今天我遇到了错误代码 -1001的问题,在这个令人费解和断断续续的问题列表中又增加了一个。我发现,苹果公司仍然没有对此负责。

编辑:

我怀疑这些错误代码是随机生成的,并且只能表明 Sandbox 已经关闭。还有其他人遇到过这个问题吗?

143949 次浏览

I have similar problems, in my case seem to be related to network connectivity:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."

Things to check:

  • Is there any chance that your server is not able to respond within some time limit? Like 60 seconds or 4 minutes?
  • Is there a possibility that your device is switching networks (WiFi, 3G, VPN)?
  • Could someone (client vs. server) be waiting for authentication?

Sorry, no ideas how to fix. Just debugging this, trying to find out what the problem is (-1021, -1001, -1009)

Update: Google search was very kind to find this:

  • -1001 TimedOut - it took longer than the timeout which was alotted.
  • -1003 CannotFindHost - the host could not be found.
  • -1004 CannotConnectToHost - the host would not let us establish a connection.

All error codes are on "CFNetwork Errors Codes References" on the documentation (link)

A small extraction for CFURL and CFURLConnection Errors:

  kCFURLErrorUnknown   = -998,
kCFURLErrorCancelled = -999,
kCFURLErrorBadURL    = -1000,
kCFURLErrorTimedOut  = -1001,
kCFURLErrorUnsupportedURL = -1002,
kCFURLErrorCannotFindHost = -1003,
kCFURLErrorCannotConnectToHost    = -1004,
kCFURLErrorNetworkConnectionLost  = -1005,
kCFURLErrorDNSLookupFailed        = -1006,
kCFURLErrorHTTPTooManyRedirects   = -1007,
kCFURLErrorResourceUnavailable    = -1008,
kCFURLErrorNotConnectedToInternet = -1009,
kCFURLErrorRedirectToNonExistentLocation = -1010,
kCFURLErrorBadServerResponse             = -1011,
kCFURLErrorUserCancelledAuthentication   = -1012,
kCFURLErrorUserAuthenticationRequired    = -1013,
kCFURLErrorZeroByteResource        = -1014,
kCFURLErrorCannotDecodeRawData     = -1015,
kCFURLErrorCannotDecodeContentData = -1016,
kCFURLErrorCannotParseResponse     = -1017,
kCFURLErrorInternationalRoamingOff = -1018,
kCFURLErrorCallIsActive               = -1019,
kCFURLErrorDataNotAllowed             = -1020,
kCFURLErrorRequestBodyStreamExhausted = -1021,
kCFURLErrorFileDoesNotExist           = -1100,
kCFURLErrorFileIsDirectory            = -1101,
kCFURLErrorNoPermissionsToReadFile    = -1102,
kCFURLErrorDataLengthExceedsMaximum   = -1103,

I use the following method in my project

-(NSArray*)networkErrorCodes
{
static NSArray *codesArray;
if (![codesArray count]){
@synchronized(self){
const int codes[] = {
//kCFURLErrorUnknown,     //-998
//kCFURLErrorCancelled,   //-999
//kCFURLErrorBadURL,      //-1000
//kCFURLErrorTimedOut,    //-1001
//kCFURLErrorUnsupportedURL, //-1002
//kCFURLErrorCannotFindHost, //-1003
kCFURLErrorCannotConnectToHost,     //-1004
kCFURLErrorNetworkConnectionLost,   //-1005
kCFURLErrorDNSLookupFailed,         //-1006
//kCFURLErrorHTTPTooManyRedirects,    //-1007
kCFURLErrorResourceUnavailable,     //-1008
kCFURLErrorNotConnectedToInternet,  //-1009
//kCFURLErrorRedirectToNonExistentLocation,   //-1010
kCFURLErrorBadServerResponse,               //-1011
//kCFURLErrorUserCancelledAuthentication,     //-1012
//kCFURLErrorUserAuthenticationRequired,      //-1013
//kCFURLErrorZeroByteResource,        //-1014
//kCFURLErrorCannotDecodeRawData,     //-1015
//kCFURLErrorCannotDecodeContentData, //-1016
//kCFURLErrorCannotParseResponse,     //-1017
kCFURLErrorInternationalRoamingOff, //-1018
kCFURLErrorCallIsActive,                //-1019
//kCFURLErrorDataNotAllowed,              //-1020
//kCFURLErrorRequestBodyStreamExhausted,  //-1021
kCFURLErrorFileDoesNotExist,            //-1100
//kCFURLErrorFileIsDirectory,             //-1101
kCFURLErrorNoPermissionsToReadFile,     //-1102
//kCFURLErrorDataLengthExceedsMaximum,     //-1103
};
int size = sizeof(codes)/sizeof(int);
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i=0;i<size;++i){
[array addObject:[NSNumber numberWithInt:codes[i]]];
}
codesArray = [array copy];
}
}
return codesArray;
}

Then I just check the error code and show alert if it is in the list

if ([[self networkErrorCodes] containsObject:[NSNumber
numberWithInt:[error code]]]){
// Fire Alert View Here
}

But as you can see I commented out codes that I think does not fit to my definition of NO INTERNET. E.g the code of -1012 (Authentication fail.) You may edit the list as you like.

In my project I use it at username/password entering from user. And in my view (physical) network connection errors could be the only reason to show alert view in your network based app. In any other case (e.g. incorrect username/password pair) I prefer to do some custom user friendly animation, OR just repeat the failed attempt again without any attention of the user. Especially if the user didn't explicitly initiated a network call.

Regards to martinezdelariva for a link to documentation.

see NSURLError.h Define

NSURLErrorUnknown =             -1,
NSURLErrorCancelled =           -999,
NSURLErrorBadURL =              -1000,
NSURLErrorTimedOut =            -1001,
NSURLErrorUnsupportedURL =          -1002,
NSURLErrorCannotFindHost =          -1003,
NSURLErrorCannotConnectToHost =         -1004,
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,
NSURLErrorAppTransportSecurityRequiresSecureConnection NS_ENUM_AVAILABLE(10_11, 9_0) = -1022,
NSURLErrorFileDoesNotExist =        -1100,
NSURLErrorFileIsDirectory =         -1101,
NSURLErrorNoPermissionsToReadFile =     -1102,
NSURLErrorDataLengthExceedsMaximum NS_ENUM_AVAILABLE(10_5, 2_0) =   -1103,


// SSL errors
NSURLErrorSecureConnectionFailed =      -1200,
NSURLErrorServerCertificateHasBadDate =     -1201,
NSURLErrorServerCertificateUntrusted =  -1202,
NSURLErrorServerCertificateHasUnknownRoot = -1203,
NSURLErrorServerCertificateNotYetValid =    -1204,
NSURLErrorClientCertificateRejected =   -1205,
NSURLErrorClientCertificateRequired =   -1206,
NSURLErrorCannotLoadFromNetwork =       -2000,


// Download and file I/O errors
NSURLErrorCannotCreateFile =        -3000,
NSURLErrorCannotOpenFile =          -3001,
NSURLErrorCannotCloseFile =         -3002,
NSURLErrorCannotWriteToFile =       -3003,
NSURLErrorCannotRemoveFile =        -3004,
NSURLErrorCannotMoveFile =          -3005,
NSURLErrorDownloadDecodingFailedMidStream = -3006,
NSURLErrorDownloadDecodingFailedToComplete =-3007,


NSURLErrorInternationalRoamingOff NS_ENUM_AVAILABLE(10_7, 3_0) =         -1018,
NSURLErrorCallIsActive NS_ENUM_AVAILABLE(10_7, 3_0) =                    -1019,
NSURLErrorDataNotAllowed NS_ENUM_AVAILABLE(10_7, 3_0) =                  -1020,
NSURLErrorRequestBodyStreamExhausted NS_ENUM_AVAILABLE(10_7, 3_0) =      -1021,


NSURLErrorBackgroundSessionRequiresSharedContainer NS_ENUM_AVAILABLE(10_10, 8_0) = -995,
NSURLErrorBackgroundSessionInUseByAnotherProcess NS_ENUM_AVAILABLE(10_10, 8_0) = -996,
NSURLErrorBackgroundSessionWasDisconnected NS_ENUM_AVAILABLE(10_10, 8_0)= -997,

I found a new error code which is not documented above: CFNetworkErrorCode -1022

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."