如何测试app store购买收据以获取原始应用程序版本
我想在应用启动时加载应用购买收据。 我如何模拟应用程序购买(不是应用程序内购买,而是实际的应用程序购买),以便我有一张收据? (我正试图从付费到免费增值)。
我正在使用此代码加载收据
(BOOL)isAppPreviouslyPurchased { BOOL wasPreviouslyPurchased = false; // Load the receipt from the app bundle. NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL]; if (receiptData) { //read purchase version from receipt NSDictionary *receipt = [NSJSONSerialization JSONObjectWithData:receiptData options:0 error:nil]; NSString *oldVersion = receipt[@"original_application_version"]; float vFloat = [oldVersion floatValue]; if (vFloat < 1.6) { wasPreviouslyPurchased = true; } } return wasPreviouslyPurchased; }
首先: 刷新收据
SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init]; request.delegate = self; [request start];
添加SKPaymentTransactionObserver
协议和此方法
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for (SKPaymentTransaction *transaction in transactions) { switch (transaction.transactionState) { // Call the appropriate custom method for the transaction state. case SKPaymentTransactionStatePurchasing: [self showTransactionAsInProgress:transaction deferred:NO]; break; case SKPaymentTransactionStateDeferred: [self showTransactionAsInProgress:transaction deferred:YES]; break; case SKPaymentTransactionStateFailed: [self failedTransaction:transaction]; break; case SKPaymentTransactionStatePurchased: [self completeTransaction:transaction]; break; case SKPaymentTransactionStateRestored: [self restoreTransaction:transaction]; break; default: // For debugging NSLog(@"Unexpected transaction state %@", @(transaction.transactionState)); break; } } }
然后,当调用此方法时,您的收据将被刷新;)
其次:您必须解密收据
NSData *receipt; // Sent to the server by the device // Create the JSON object that describes the request 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://buy.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]; if (!jsonResponse) { /* ... Handle error ...*/ } /* ... Send a response back to the device ... */ } }];
您可以使用此代码对其进行解密,但Apple并未真正推荐它。 您应该从服务器调用iTunes。
然后,您可以使用Apple服务器返回的响应调用您的方法。
像这样(通过本地validation,Apple说的糟糕方式)
NSData *receipt; // Sent to the server by the device // Create the JSON object that describes the request 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://buy.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]; if (!jsonResponse) { /* ... Handle error ...*/ } [self isAppPreviouslyPurchased:jsonResponse]; } }]; -(BOOL)isAppPreviouslyPurchased:(NSDictionary *)receipt { BOOL wasPreviouslyPurchased = false; NSString *oldVersion = receipt[@"original_application_version"]; float vFloat = [oldVersion floatValue]; if (vFloat < 1.6) { wasPreviouslyPurchased = true; } return wasPreviouslyPurchased; }
- 填充antialiased聚cocos2d
- iOS CoreLocation:有可能知道位置数据是来自GPS还是来自Wifi?
- 无法将typesPromise(_,_) – > DataRequest的返回expression式转换为返回typesPromise <DataResponse,AnyObject >>
- 用简单的英语-什么是SDK? –拉克兰·柯克伍德–中
- CustomCell为空或导致SIGABRT错误
- ViewWillDisappear没有被称为searchcontroller
- 在iOS中,如何从正在运行的应用程序中获取当前的CPU利用率?
- XCode unittest构建失败,出现错误“架构x86_64的未定义符号”
- “ErrorType”不能转换为“NSError”