2017-07-04 122 views
0

我跟着這個鏈接做應用程序購買(http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/comment-page-20/#comment-22550)。使用沙箱測試用戶(與美國領土)我可以成功購買產品。如果我再次按購買按鈕,需要恢復產品。我怎麼知道產品是否已經購買?我需要驗證自動續訂訂閱的收據。我能怎麼做?使用目標c。在應用程序購買恢復不起作用

+0

您是否在使用Non-Consumable In App購買? – Surjeet

+0

不,我正在使用自動更新訂閱IAP – Ishwarya

回答

0

首先,實現一個定製的支付類符合協議SKPaymentTransactionObserver。這個類可以用來接收支付信息:

.H:

#import <StoreKit/StoreKit.h> 

@interface PaymentObserver : NSObject<SKPaymentTransactionObserver> 
+ (instancetype)sharedInstance; 
@end 

.M:

@implementation PaymentObserver 

+ (instancetype)sharedInstance{ 
    static PaymentObserver * instance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     instance = [[PaymentObserver alloc] init]; 
    }); 
    return instance; 
} 

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions { 
    for (SKPaymentTransaction *transaction in transactions) { 
     switch (transaction.transactionState) { 
       // Call the appropriate custom method for the transaction state. 
      case SKPaymentTransactionStatePurchasing: 
       break; 
      case SKPaymentTransactionStateDeferred: 
       break; 
      case SKPaymentTransactionStateFailed: 
       break; 
      case SKPaymentTransactionStatePurchased: 
       break; 
      case SKPaymentTransactionStateRestored: 
       //Purchased product info goes here. Get more details in transaction object. 
       break; 
      default: 
       break; 
     } 
    } 
} 

然後,在AppDelegate,使用上面的類來檢索支付信息:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    //...... 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:[PaymentObserver sharedInstance]]; 
    //...... 
} 

PaymentObserver類將從AppStore獲取購買的產品信息。

主要程序是這樣的。有更多的細節你要處理它們。

+0

Thanks.it很有用。你可以給我完整的源代碼或任何參考鏈接? – Ishwarya

+0

我們只是爲了學習而創建了一個類,沒有太多實際的功能,並且從項目中分離時可能不會編譯源代碼文件。如果你仍然想要,請留下電子郵件。 –