2011-05-30 74 views
6

我在iOS應用中使用應用內購買的可更新訂閱。 當用戶嘗試購買他們已經爲消息付費的訂閱時,iTunes會顯示「您當前訂閱了此信息」。已在StoreKit中購買訂閱

如何檢測此事件何時發生,以便我可以處理事務並授予對我的應用程序的訪問權限。

在paymentQueue:updatedTransactions:它作爲SKPaymentTransactionStateFailed傳入的觀察者的方法。我如何區分這種類型的故障和其他故障,如用戶按取消按鈕?

我是否會提交已返回的事務或是否需要調用restorePreviousTransactions。

在Apple文檔中,它聲明「如果用戶試圖購買非消費品或已購買的可續訂訂閱,您的應用程序會收到該項目的常規交易,而不是恢復交易。但是,用戶不會再爲該產品收取費用,您的應用程序應將這些交易視爲與原始交易相同。「

回答

0
Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app. 

時,通過與蘋果的驗證(我用的php網站的代碼來做到這一點),你會得到一個「身份碼」迴響應,並可以驗證它是否是一個代碼21006(訂閱是存在訂閱,檢測已過期)或其他人(我認爲除0和21006以外的任何內容都是實際的錯誤)。

我做事情的方式是將交易詳細信息存儲在PLIST文件中,該文件存儲在文檔目錄中。

你可以到PLIST添加額外的領域,如expiryDates,布爾標誌等

這種方式,你有收據的複印件,但你應該總是驗證它,因爲它可能已過期。

Q:在paymentQueue:updatedTransactions:它 即將通過作爲SKPaymentTransactionStateFailed觀察者的方法。我如何 區分這種類型的故障和其他故障,如 用戶按取消按鈕?

在updatedTransactions方法中使用switch語句來確定不同類型的響應。

-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error 
{  
    NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription]; 
    NSLog(@"Error - %@", message); 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:message 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
} 

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
{ 
    NSLog(@"updatedTransactions"); 
    for (SKPaymentTransaction *transaction in transactions) 
    { 
     switch (transaction.transactionState) 
     { 
      case SKPaymentTransactionStatePurchasing: 
       // take action whilst processing payment 
       break; 

      case SKPaymentTransactionStatePurchased: 
       // take action when feature is purchased 
       break; 

      case SKPaymentTransactionStateRestored: 
       // take action to restore the app as if it was purchased    
       break; 


      case SKPaymentTransactionStateFailed: 
       if (transaction.error.code != SKErrorPaymentCancelled) 
       { 
       // Do something with the error 
       } // end if 
       break; 

      default: 
       break; 
     } // end switch 

    } // next 

的TransactionStateFailed處理失敗,雖然我不代碼取消,因爲沒有理由讓我在我的應用程序這樣做。

問:我是否提交退回的交易或是否需要致電 restorePreviousTransactions。

我相信StoreKit處理這種在內部與finishTransaction方法和restorePreviousTransaction方法

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

玩完交易

我希望這有助於