2011-08-15 101 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č同樣的事情發生,你的修復是偉大的。 –

相關問題