前言:關於In-App Purchase(內購)的注意事項
一點注意事項:
- 內購應該有游客身份的購買選項,不然可能被拒,前一段時間我這邊一直是因為沒有游客身份購買被拒。
- 在驗證內購結果的時候要注意使用下列的代碼,否則之前遇到過一個21002的狀態碼,這個狀態碼說是receipt-data的內容有問題,不過用了官方給的代碼后是可以正常的驗證的
關鍵代碼如下:

1 NSData *receipt; // Sent to the server by the device 2 3 // Create the JSON object that describes the request 4 NSError *error; 5 NSDictionary *requestContents = @{ 6 @"receipt-data": [receipt base64EncodedStringWithOptions:0] 7 }; 8 NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents 9 options:0 10 error:&error]; 11 12 if (!requestData) { /* ... Handle error ... */ } 13 14 // Create a POST request with the receipt data. 15 NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"]; 16 NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL]; 17 [storeRequest setHTTPMethod:@"POST"]; 18 [storeRequest setHTTPBody:requestData]; 19 20 // Make a connection to the iTunes Store on a background queue. 21 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 22 [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue 23 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 24 if (connectionError) { 25 /* ... Handle error ... */ 26 } else { 27 NSError *error; 28 NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 29 if (!jsonResponse) { /* ... Handle error ...*/ } 30 /* ... Send a response back to the device ... */ 31 } 32 }];
參考網址:
In-App Purchase Programming Guide
Validating Receipts With the App Store(這個鏈接下有關於驗證訂單的代碼 狀態碼 等信息)
iOS交流群歡迎你的加入!
群二維碼:
先寫到這么多
如有問題,敬請指正;
如需轉載,請注明出處,謝謝!