2

我正在在 OSX 应用上进行应用内购买验证并遇到一些问题~

我的请求代码是这样的:

    NSData* data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
    NSString* base64Str = [data base64EncodedString];

    NSDictionary* dict = [NSDictionary dictionaryWithObject:base64Str forKey:@"receipt-data"];
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString* jsonStr = [jsonParser stringWithObject:dict error:nil];
    postUrl = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

    _verifyRequest = [[NSMutableURLRequest alloc] init];
    [_verifyRequest setTimeoutInterval:_sTimeoutInterval];
    [_verifyRequest setURL:postUrl];
    [_verifyRequest setHTTPBody:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
    [_verifyRequest setHTTPMethod:ASCommon_HttpMethodPost];
    _verifyConnection = [[NSURLConnection alloc] initWithRequest:_verifyRequest delegate:self];
    [_verifyConnection start];

我得到这样的结果

{
    exception = "java.lang.IllegalArgumentException";
    status = 21002;
}

而且我记录的 jsonStr 变量似乎没有问题:

{"receipt-data":"..........................................................."}

Apple doc here说“检索收据数据。......在 OS X 上,这是应用程序包内收据文件的全部内容。使用 base64 编码对收据数据进行编码。”

我是否误解收据数据是文件的全部内容?还是我的代码有问题?

4

1 回答 1

1

我想我找到了解决方案。默认情况下,base64 编码的字符串类不会删除换行符。您需要更改它以删除换行符。更改此功能:

- (NSString *)base64EncodedString
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], true, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

- (NSString *)base64EncodedString : (BOOL)includeLineBreaks
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], includeLineBreaks, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

并在参数中传递 NO。

于 2013-09-24T04:58:44.877 回答