如果我購買東西并檢查購買收據,我可以通過以下方式獲得它的編碼值:
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
print(receiptData)
let receiptString = receiptData.base64EncodedString(options: [])
print("receiptString \(receiptString)")
// Read receiptData
}
catch { print("Couldn't read receipt data with error: " error.localizedDescription) }
}
但是如果我購買了東西,然后洗掉應用程式并重新安裝并檢查收據,我無法使用上面的代碼得到任何東西。它只是得到appStoreReceiptURL 并直接離開而不進入do {} catch。在這種情況下如何獲得收據?
uj5u.com熱心網友回復:
如果您嘗試恢復購買,將不會退還用戶購買的金額,但是您將能夠驗證用戶是否購買了該產品,并且還會收到包含有關他之前購買的所有資訊的收據,因此您可以安全地驗證在您的應用程式中購買。
在 App Store Review Guides 中明確指出
通過應用內購買購買的任何積分或游戲內貨幣可能不會過期,您應確保您有任何可恢復的應用內購買的恢復機制。
這意味著無論用戶卸載應用程式的情況如何,您都應該提供一些機制(例如恢復購買按鈕)。在您的情況下,您可以直接在登錄螢屏上提供恢復按鈕。在Apple 人機界面指南中查看有關恢復購買按鈕的一些靈感。
我想在用戶點擊恢復購買按鈕后直接使用 Apple API 恢復購買,您可以使用:
SKPaymentQueue.default().restoreCompletedTransactions()
,which triggers Apple servers, checks the users Apple ID and gets all of the app purchases they have made before and returns you transaction that you observe with SKPaymentTransactionObserver with this code:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
if transaction.transactionState == .restored {
//here is your restored transaction
}
}
}
If you however want to get hold of the new receipt with all receipt data, including previous purchases, I recommend not using directly Apple API, but using SwiftyStoreKit library : link on github . Code example below:
func restoreYourPurchases( completion: @escaping (Result<Bool,Error>) -> Void) {
SwiftyStoreKit.restorePurchases(atomically: true) { results in
var restoreSuccessfull = false
if results.restoreFailedPurchases.count > 0 {
print("Restore Failed: \(results.restoreFailedPurchases)")
}
else if results.restoredPurchases.count > 0 {
print("Restore Success: \(results.restoredPurchases)")
for purchase in results.restoredPurchases {
if purchase.productId == "yourID" {
let timeOfPurchase = purchase.originalPurchaseDate
}
}
}
else {
print("Nothing to Restore")
completion(.success(false))
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/421738.html
標籤:
