造個輪子,由於有個swift版本SwiftyStoreKit,沒有找到oc版本。html
swift內購很強大庫:github.com/bizz84/Swif…ios
付費下載,版本刪除後從新下載,這時候咱們app改爲了內購版本,須要判斷是不是以前付費下載用戶,而後默認打開購買權益。git
1)獲取Receipt數據, 若是本地有直接獲取。github
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData = [NSData dataWithContentsOfURL:receiptUrl];
複製代碼
若是本地沒有,去蘋果刷新,而後再從本地獲取。json
SKReceiptRefreshRequest *refreshReqeust = [[SKReceiptRefreshRequest alloc] init];
refreshReqeust.delegate = self;
[refreshReqeust start];
複製代碼
蘋果驗證域名:swift
NSString *production = @"https://buy.itunes.apple.com/verifyReceipt"; // 正式環境 NSString *sandbox = @"https://sandbox.itunes.apple.com/verifyReceipt"; // 沙盒環境 複製代碼
須要將Receipt,base64編碼成string,請求參數json以下:bash
key | value |
---|---|
receipt-data | base64 編碼的收據數據。 |
password | 僅用於包含自動續期訂閱的收據。您App的共享密鑰(十六進制字符串)。 |
exclude-old-transactions | 僅用於包含自動續期訂閱或非續期訂閱的iOS7樣式App收據。若是值爲 true,僅響應包括全部訂閱的最新續期交易。 |
獲取蘋果返回結果以下:markdown
{ environment = Sandbox; receipt = { "adam_id" = 0; "app_item_id" = 0; "application_version" = "1.1.0"; "bundle_id" = "com.xxx"; "download_id" = 0; "in_app" = ( // App 內購買項目收據字段 { "is_trial_period" = false; "original_purchase_date" = "2018-12-13 06:43:11 Etc/GMT"; "original_purchase_date_ms" = 1544683391000; "original_purchase_date_pst" = "2018-12-12 22:43:11 America/Los_Angeles"; "original_transaction_id" = 1000000486729404; "product_id" = "com.xxx"; "purchase_date" = "2018-12-13 06:43:11 Etc/GMT"; "purchase_date_ms" = 1544683391000; "purchase_date_pst" = "2018-12-12 22:43:11 America/Los_Angeles"; quantity = 1; "transaction_id" = 1000000486729404; } ); "original_application_version" = "1.0"; // 最初購買的 App 的版本 "original_purchase_date" = "2013-08-01 07:00:00 Etc/GMT"; "original_purchase_date_ms" = 1375340400000; "original_purchase_date_pst" = "2013-08-01 00:00:00 America/Los_Angeles"; "receipt_creation_date" = "2018-12-14 12:28:12 Etc/GMT"; "receipt_creation_date_ms" = 1544790492000; "receipt_creation_date_pst" = "2018-12-14 04:28:12 America/Los_Angeles"; "receipt_type" = ProductionSandbox; "request_date" = "2018-12-18 07:10:21 Etc/GMT"; "request_date_ms" = 1545117021021; "request_date_pst" = "2018-12-17 23:10:21 America/Los_Angeles"; "version_external_identifier" = 0; }; status = 0; } 複製代碼
經過解析最初購買app的版本original_application_version,跟內購第一個版本進行比較,判斷是否是須要恢復購買身份app
in_app字段下是內購的信息,能夠用來判斷是否是內購過。ide
蘋果官方文檔: developer.apple.com/cn/app-stor…
本文代碼git地址: github.com/342261733/X…