Is there any way to know purchase made by which iTunes account? - iOS

江枫思渺然 提交于 2019-12-12 08:03:56

问题


My app provides an in-app-purchase non-consumable type. The app has log in facility. Is it possible to make purchase based on logged in user to my app?

I know it sounds awkward. Let me specify the problem.

The device has a already logged in user at iTunes store. At that time, when my app gets install to device for first time, any user can login with credentials to my app. Consider, he made some purchase and log out from the app. Now, another user logged in to the app and can't use facilities available for previous user after in app purchase. But as you know non-consumable product purchase based on iTunes store account (only one non-consumable product can be purchased for each account). I have tried by following way..

  1. Store user id to iCloud, but I don't know by which iTunes account that purchase was made. So, I couldn't display proper message "You should log in with different iTunes account to make purchase".

  2. In android, with the In-app Billing v3, a developer payload string can be sent along with the purchase request. This string is also returned when a query is made for that purchase. So, this can be used to identify the user who made the purchase. Is there anything similar available in iOS?


回答1:


As of iOS 7, SKMutablePayment has an applicationUsername property that can be set when a payment is made. And, SKPaymentQueue has the restoreCompletedTransactionsWithApplicationUsername: method (link).

Apple's guidance is to hash your server-side user ID and pass that up on purchase and restore. So, in your case, if iTunes User A purchases the product it will be bound to your server-side user ID for that purchaser. Then, if iTunes User B attempts to restore, restoration will fail, because they would still be passing up the same server-side user ID.

You'll also have to track (on the server) that your server-side user ID has purchased the product. Otherwise, if iTunes User B attempts to restore you'll need to know the difference between they having logged into the device with a different iTunes account and they having never purchased. And of course, you'll also want to prevent the same server-side user from purchasing the product twice, under different iTunes accounts.

Caveats:

  • If you already have purchases made in production, this most likely won't work.
  • iOS 7+
  • The "already purchased" scenario I mentioned above.



回答2:


if restore purchase is success than purchased made by this account.

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];


// Then this is called
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    NSLog(@"%@",queue );
    NSLog(@"Restored Transactions are once again in Queue for purchasing %@",[queue transactions]);  

    NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
    NSLog(@"received restored transactions: %i", queue.transactions.count);

    for (SKPaymentTransaction *transaction in queue.transactions) {
        NSString *productID = transaction.payment.productIdentifier;
        [purchasedItemIDs addObject:productID];
        NSLog (@"product id is %@" , productID);
        // here put an if/then statement to write files based on previously purchased items
        // example if ([productID isEqualToString: @"youruniqueproductidentifier]){write files} else { nslog sorry}
    }  
}


来源:https://stackoverflow.com/questions/29255568/is-there-any-way-to-know-purchase-made-by-which-itunes-account-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!