2011-08-15 99 views
1

我的应用程序FourFourTwo Stats Zone刚刚去住在App Store今天晚上:iPhone 3G 4.2.1 - SKProductsRequest委托方法不会被调用

我问了几个人来测试应用程序内购买和获得成功除iPhone 3G之外的所有设备(运行4.2.1 - 尚未与其他iOS版本进行测试)。我试过在我有的设备上调试它,看起来没有任何SKProductsRequest委托方法被调用。这里是我的代码:

- (void)requestPurchaseOfCompetition:(Competition*)competition { 
    DLog(""); 

    if ([SKPaymentQueue canMakePayments]) { 
     DLog(@"do store"); 

     NSString* productIdentifier = [NSString stringWithFormat:@"%@%@_%@", kPRODUCT_IDENTIFIER_PREFIX, competition.competitionId, competition.season]; 

     SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:productIdentifier]]; 

     [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:request forKey:@"request"]]]; 
     request.delegate = self; 
     [request start]; 
     [request release]; 
    } else { 
     DLog(@"no store"); 

     // Warn the user that purchases are disabled. 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Store", @"Store") message:NSLocalizedString(@"In app purchasing is disabled for this device (in Settings > General > Restrictions). Please enable this setting to purchase more competitions.", @"In app purchasing is disabled for this device (in Settings > General > Restrictions). Please enable this setting to purchase more competitions.") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 
} 

... 

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { 
    DLog(@"response: %@", response); 
    DLog(@"invalid product identifiers: %@", response.invalidProductIdentifiers); 

    for (SKProduct *product in response.products) { 
     DLog(@"product: %@", product); 

     [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationGotProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:product forKey:@"product"]]]; 

     SKPayment *payment = [SKPayment paymentWithProduct:product]; 

     SKPaymentQueue *paymentQueue = [SKPaymentQueue defaultQueue]; 
     [paymentQueue addTransactionObserver:self]; 
     [paymentQueue addPayment:payment]; 
    } 
} 

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error { 
    DLog(@"request failed: %@, %@", request, error); 

    [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfoFailed object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]]]; 
} 

- (void)requestDidFinish:(SKRequest *)request { 
    DLog(@"request finished: %@", request); 
} 

这三个委托方法中的日志消息都没有出现。

这适用于3GS,iPhone 4,iPad等,但不适用于运行4.2.1的iPhone 3G。

任何人都可以提供任何见解吗?

回答

2

在开始请求后,您不应该立即发布SKProductsRequest *request,但应该已经在两个委托方法中释放它。检查the documentationSKRequest类,它说:

当这个方法(requestDidFinishrequest:didFailWithError:) 被调用,您的代理接收来自 要求没有进一步的通信,并可以将其释放。

我不知道为什么在较新版本的SDK中为你工作,但严格看你的代码,请求可能在响应可能调用委托方法之前被释放。

这是我会怎么做:

- (void)requestPurchaseOfCompetition:(Competition*)competition { 
    DLog(""); 

    if ([SKPaymentQueue canMakePayments]) { 
     DLog(@"do store"); 

     NSString* productIdentifier = [NSString stringWithFormat:@"%@%@_%@", kPRODUCT_IDENTIFIER_PREFIX, competition.competitionId, competition.season]; 

     SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:productIdentifier]]; 

     [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:request forKey:@"request"]]]; 
     request.delegate = self; 
     [request start]; 

    } else { 
     DLog(@"no store"); 

     // Warn the user that purchases are disabled. 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Store", @"Store") message:NSLocalizedString(@"In app purchasing is disabled for this device (in Settings > General > Restrictions). Please enable this setting to purchase more competitions.", @"In app purchasing is disabled for this device (in Settings > General > Restrictions). Please enable this setting to purchase more competitions.") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 
} 

... 

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { 
    DLog(@"response: %@", response); 
    DLog(@"invalid product identifiers: %@", response.invalidProductIdentifiers); 

    for (SKProduct *product in response.products) { 
     DLog(@"product: %@", product); 

     [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationGotProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:product forKey:@"product"]]]; 

     SKPayment *payment = [SKPayment paymentWithProduct:product]; 

     SKPaymentQueue *paymentQueue = [SKPaymentQueue defaultQueue]; 
     [paymentQueue addTransactionObserver:self]; 
     [paymentQueue addPayment:payment]; 
    } 
    [request release]; 
} 

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error { 
    DLog(@"request failed: %@, %@", request, error); 

    [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfoFailed object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]]]; 
    [request release]; 
} 
+0

感谢@MatejBalantič同样的事情发生,你的修复是伟大的。 –

相关问题