2012-11-05 35 views
0

我注意到,如果您在我的應用程序中輸入我的商店視圖(詢問蘋果產品),然後在所有產品從蘋果加載之前關閉商店視圖,它會崩潰。In App Purchase關閉商店視圖後崩潰

2012-11-05 12:32:08.420 Bellman[71368:c07] Dealloc inAppManager 
2012-11-05 12:32:13.788 Bellman[71368:c07] *** -[InAppPurchaseManager respondsToSelector:]: message sent to deallocated instance 0x8e85a50 

正如你在我的代碼中看到的,當調用dealloc時,我打電話給removeTransactionObserver:。正如你在日誌中看到的,dealloc在應用程序崩潰前5秒被調用。從我所瞭解的默認隊列試圖呼叫productsRequest: didReceiveResponse:即使我已經刪除了我的自我觀察員?

- (void)requestProUpgradeProductData:(NSSet*)productIdentifiers 
{ 
    self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; 
    self.productsRequest.delegate = self; 
    [self.productsRequest start]; 

} 

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
{ 
    NSArray *products = response.products; 
    NSLog(@"%@",products); 

    for (NSString *invalidProductId in response.invalidProductIdentifiers) 
    { 
     NSLog(@"Invalid product id: %@" , invalidProductId); 
    } 

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil]; 
    [[self delegate] didLoadStore:response.products]; 
} 

- (void)loadStore:(NSSet*)productIdentifiers 
{ 
    // restarts any purchases if they were interrupted last time the app was open 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 

    // get the product description (defined in early sections) 
    [self requestProUpgradeProductData:productIdentifiers]; 
    NSLog(@"Unfinished Transactions: [%i]", [[SKPaymentQueue defaultQueue].transactions count]); 
} 

- (BOOL)canMakePurchases 
{ 
    return [SKPaymentQueue canMakePayments]; 
} 

- (void)purchase:(SKProduct*)product { 
    SKPayment *payment = [SKPayment paymentWithProduct:product]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
} 


- (void)recordTransaction:(SKPaymentTransaction *)transaction 
{ 
    NSLog(@"Subscription bought"); 
} 


- (void)provideContent:(SKPaymentTransaction *)transaction 
{ 
    NSLog(@"Asking user to register Database"); 
    [[self delegate] provideContent:transaction]; 
} 

- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful 
{ 
    // remove the transaction from the payment queue. 
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
    NSLog(@"Removeing recipt"); 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil]; 
    if (wasSuccessful) 
    { 
     [[self delegate] didMakePurchase:transaction.payment.productIdentifier]; 
     // send out a notification that we’ve finished the transaction 
     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo]; 
    } 
    else 
    { 
     // send out a notification for the failed transaction 
     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo]; 
    } 
} 

- (void)completeTransaction:(SKPaymentTransaction *)transaction 
{ 
    [self recordTransaction:transaction]; 
    [self provideContent:transaction]; 
} 

- (void)restoreTransaction:(SKPaymentTransaction *)transaction 
{ 

} 

- (void)completePurches:(SKPaymentTransaction *)transaction { 
    NSLog(@"EVERYTHING IS DONE!"); 
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"unfinishedReceipt"]; 
    [self finishTransaction:transaction wasSuccessful:YES]; 
} 

- (void) failedTransaction: (SKPaymentTransaction *)transaction { 
    if (transaction.error.code != SKErrorPaymentCancelled) 
    { 
     [[self delegate] didReciveAppStoreError:transaction.error.localizedDescription]; 
     // Optionally, display an error here. 
    } 
    [[self delegate] didCancelPurches]; 
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
} 


- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
{ 
    for (SKPaymentTransaction *transaction in transactions) 
    { 
     switch (transaction.transactionState) 
     { 
      case SKPaymentTransactionStatePurchased: 
       NSLog(@"Completed transcation"); 
       [self completeTransaction:transaction]; 

       break; 
      case SKPaymentTransactionStateFailed: 
       NSLog(@"Failed transcation: %@",transaction.error); 
       [self failedTransaction:transaction]; 
       break; 
      case SKPaymentTransactionStateRestored: 
       NSLog(@"Restore transcation: %@",transaction.payment.productIdentifier); 
       [self restoreTransaction:transaction]; 
       break; 
      case SKPaymentTransactionStatePurchasing: 
       NSLog(@"ID: %@",transaction.transactionIdentifier); 
       break; 
      default: 
       break; 
     } 
    } 
} 

-(void)didVerifyNewRecipt:(SKPaymentTransaction *)recipt wasNewRecipit:(BOOL)status { 
    if (status) { 
     NSLog(@"Was new recipt showing interface"); 
     [self completeTransaction:recipt]; 
    } else { 
     NSLog(@"Was old Receipt"); 
     [self completePurches:recipt]; 
     [[self delegate] didFindOldReceipt]; 
    } 
} 

-(void)didReciveError:(NSString *)error { 
    NSLog(@"ERROR! %@",error); 
} 

-(void)dealloc { 
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 
    NSLog(@"Dealloc inAppManager"); 
} 

回答

2

當我打算「問問題」時,它只是想出了它。由於我沒有在互聯網上找到答案,我會分享我的解決方案。

由於SKProductsRequest不是SKPaymentQueue的一部分,並且SKProductsRequest實現了它們自己的委託,因此您需要刪除您在SKProductsRequest上設置的委託。

-(void)dealloc { 
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 
    self.productsRequest.delegate = nil; // <----- Solution 
    NSLog(@"Dealloc inAppManager"); 
}