ios inapp购买收据

时间:2016-06-10 10:16:22

标签: ios in-app-purchase

ios应用程序内购买如何知道此产品已经购买(我可以在nsuserdefault本地查看,但是从手机中删除应用后如何知道,然后重新安装应用程序)

https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式收据(来自 Read the Receipt Data):

// Load the receipt from the app bundle.
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if (!receipt) { /* No local receipt -- handle the error. */ }

如果用户删除了该应用,则必须提供恢复购买的方法。

您可以这样做:

- (IBAction)restorePurchase:(id)sender{
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

// Then this is called
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];

    for (SKPaymentTransaction *transaction in queue.transactions) {
        NSString *productID = transaction.payment.productIdentifier;
        [purchasedItemIDs addObject:productID];
        NSLog(@"product id is %@", productID);
    }  
}