1

这是它崩溃的功能。确切的代码行是:removeObjectForKey。即使测试函数完全为空,它也会在 removeObjectForKey 上崩溃。注意:我只是传入一个空函数回调。目前,我关闭了 ARC,我需要打开它吗?如果可能的话,我想关闭 ARC,因为打开它意味着要处理很多编译问题。

该函数确实说明了非保留对象,因此可能是内存问题。

- (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]];
    [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];

    if (isOk)
    {
        //Validation suceeded. Unlock content here.
        NSLog(@"Validation successful");
        completionHandler(TRUE);

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

这是verificationController的用法:

    [[VerificationController sharedInstance] verifyPurchase:transaction completionHandler:^(BOOL success) {
        if (success) {

            NSLog(@"Hi, its success.");
            [self testMethod];

       } else {
            NSLog(@"payment not authorized.");
        }
    }];         
}

- (void) testMethod {

}

我可以使用 __weak 但我必须打开 ARC,我试图避免这种情况。注意:当我将 verificaitionController 放在其他类/对象中时它可以工作,但是一旦我将它放在 InAppPurchaseManager 中,它就会在尝试访问 self 时爆炸。Self 指向 InAppPurchaseManager 的一个实例,定义如下(它是一个 phonegap 插件):

@interface InAppPurchaseManager : CDVPlugin <SKPaymentTransactionObserver> {

}
4

4 回答 4

3

我也遇到了这个问题我以这种方式解决了这个问题主要问题是当你在 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:15:11.947 回答
0

_completionHandlers 为零吗?你可能会做这样的事情 -

if (_completionHandlers && [_completionHandlers respondsToSelector:@selector(removeObjectForKey:)]) {
    [_completionHandlers removeObjectForKey:[NSValue valueWithNonretainedObject:connection]];
}

祝你好运。

于 2013-05-09T20:37:26.853 回答
0

查找以下字符串:

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

并更改为:

[_completionHandlers setObject:[completionHandler copy] forKey:[NSValue valueWithNonretainedObject:conn]];
于 2013-06-15T19:37:11.970 回答
0

我不知道你是否找到了答案,但我刚刚意识到 _completionHandlers 从未分配过(如果你在设置断点后 po _completionHandlers 你会发现它是 nil)。希望这可以帮助!

// in VerificationController.m

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        transactionsReceiptStorageDictionary = [NSMutableDictionary dictionary];
        _completionHandlers = [NSMutableDictionary dictionary];
    }
    return self;
}
于 2013-08-13T14:12:55.270 回答