1

我正在制作一个应用程序,我必须让我的 Inappurchase 产品自动更新,为此,在阅读 Apple 文件后,我知道在每次自动更新产品交易后,我们的应用程序都会收到每次购买的交易收据,我需要在验证我的交易收据应用程序后验证来自 Apple 服务器的收据必须保存该交易日期。但是在购买产品后,当我尝试通过 Apple 类 - 验证控制器验证来自 Apple 服务器的交易收据时,我的应用程序在完成处理程序处崩溃,它显示完成处理程序 NIL。

当执行到达这些方法中的任何一个时,我的 _completionHandlers 被释放,现在怎么办?请指导我解决这个问题

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    // So we got some receipt data. Now does it all check out?
    BOOL isOk = [self doesTransactionInfoMatchReceipt:responseString];

    VerifyCompletionHandler completionHandler = _completionHandlers[[NSValue valueWithNonretainedObject:connection]];

    NSValue *key = [NSValue valueWithNonretainedObject:connection];

    NSLog(@"%@",_completionHandlers);
    [_completionHandlers removeObjectForKey:key];
    if (isOk)
    {
    //Validation suceeded. Unlock content here.
    NSLog(@"Validation successful");
    completionHandler(TRUE);
    } else {
    NSLog(@"Validation failed");
    completionHandler(FALSE);
    }
    }
4

1 回答 1

5

我也遇到了这个问题我以这种方式解决了这个问题主要问题是当你在 verifyPurchase 方法中为完成处理程序设置值时它正在设置 nil 值所以在 verifyPurchase 方法中找到这一行

_completionHandlers[[NSValue valueWithNonretainedObject:conn]] = completionHandler;

并将其替换为

[_completionHandlers setObject:[completionHandler copy] forKey:[NSValue valueWithNonretainedObject:conn]];

更改这两行将解决您的崩溃,但要确保也执行这些步骤

并找到 connectionDidReceivedata 方法并将其替换为

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    // So we got some receipt data. Now does it all check out?
    BOOL isOk = [self doesTransactionInfoMatchReceipt:responseString];


    if (_completionHandlers && [_completionHandlers respondsToSelector:@selector(removeObjectForKey:)])
    {
        VerifyCompletionHandler completionHandler = _completionHandlers[[NSValue valueWithNonretainedObject:connection]];
        [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
        if (isOk)
        {
            //Validation suceeded. Unlock content here.
            NSLog(@"Validation successful");
            completionHandler(TRUE);

        } else {
            NSLog(@"Validation failed");
            completionHandler(FALSE);
        }

    }
    //[_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
   }
于 2013-11-18T10:10:10.593 回答