iOS 7:应用内购买收据验证和确认

4
我对iOS中的收据验证不熟悉。我已经成功实现了应用内购买,现在希望在这个应用内购买中包括收据验证部分。
我的应用内购买有3个产品。我想在我的应用程序中单独购买每个产品之前进行收据验证。
为此,我按照以下链接进行操作:官方苹果开发者教程 我成功获取了收据数据,但是我对获取收据数据后该怎么做感到困惑。我如何将其发送回苹果服务器进行验证,然后开始应用内购买过程?
以下是我的代码:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

NSLog(@"transactions count : %d",transactions.count);

BOOL flgIsProductRestorable=NO;

for (SKPaymentTransaction *transaction in transactions) {
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchasing:
        {
            // show wait view here
            //statusLabel.text = @"Processing...";

            NSLog(@"Processing...");
        }
            break;

        case SKPaymentTransactionStatePurchased:
        {
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [SVProgressHUD dismiss];
            // remove wait view and unlock feature 2
            //statusLabel.text = @"Done!";
            NSLog(@"Success : %@ = %@",transaction.payment.productIdentifier,transaction.transactionIdentifier);
            NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
            NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
            NSLog(@"%@",receipt);

            NSError *error;
            NSDictionary *requestContents = @{
                                              @"receipt-data": [receipt base64EncodedStringWithOptions:0]
                                              };
            NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                                  options:0
                                                                    error:&error];

            if (!requestData) { /* ... Handle error ... */ }

            // Create a POST request with the receipt data.
            NSURL *storeURL = [NSURL URLWithString:@"https://sand.itunes.apple.com/verifyReceipt"];
            NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
            [storeRequest setHTTPMethod:@"POST"];
            [storeRequest setHTTPBody:requestData];

            // Make a connection to the iTunes Store on a background queue.
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                                       if (connectionError) {
                                           /* ... Handle error ... */
                                       } else {
                                           NSError *error;
                                           NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                           NSLog(@"response : %@",jsonResponse);
                                           if (!jsonResponse) { /* ... Handle error ...*/ }
                                           /* ... Send a response back to the device ... */
                                       }
                                   }];


            [self WS_EbookDetail:transaction.transactionIdentifier];

        }...

我刚刚从苹果链接中复制粘贴了收据检索代码。

接下来该怎么办?


使用Swift Store Kit非常简单,详情请见https://github.com/bizz84/SwiftyStoreKit。 - Awais Mobeen
1个回答

0
请注意,苹果建议您安全地将收据数据发送到您的服务器,然后调用他们的服务器。直接调用苹果的服务器是不安全的。
您还可以在应用程序中进行本地收据验证。有关如何执行此操作的信息,请参见本地验证收据
此外,还有一段非常棒的2014年WWDC视频“使用收据防止未经授权的购买”,详细介绍了在设备上实现收据验证的方法。

如果我们从本地验证收据(而不是使用自己的服务器)来远程验证应用程序,苹果能否拒绝该应用? - SoftDesigner
我认为在如今的时代,拥有自己的服务器并不是必要的,因为iOS9现在强制使用ATS。 - Ace Green

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接