我正在尝试在iOS应用中实现In App Purchase Receipt验证。我跟着this tutorial并使用作为基础提供的示例项目来处理我自己的应用程序。
这是我在这一点上面临的一个问题。在一个名为validateReceipt()的函数中:
func validateReceipt() {
......
receipt = Receipt()
if let receiptStatus = receipt?.receiptStatus { // Critical point.
...... // A lot of important things happening here.
}
......
}
按照调试器中的内容,我看到当到达注释的行时://临界点。我有,收据不是零但是:
receipt?.receiptStatus == nil
这可能是什么原因?我以为我在路上做错了什么,但不知道是什么。
为了使事情更清楚,接下来是从教程中获取的Receipt类的定义:
/// Copyright (c) 2018 Razeware LLC
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
/// distribute, sublicense, create a derivative work, and/or sell copies of the
/// Software in any work that is designed, intended, or marketed for pedagogical or
/// instructional purposes related to programming, coding, application development,
/// or information technology. Permission for such use, copying, modification,
/// merger, publication, distribution, sublicensing, creation of derivative works,
/// or sale is expressly withheld.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
import UIKit
enum ReceiptStatus: String {
case validationSuccess = "This receipt is valid."
case noReceiptPresent = "A receipt was not found on this device."
case unknownFailure = "An unexpected failure occurred during verification."
case unknownReceiptFormat = "The receipt is not in PKCS7 format."
case invalidPKCS7Signature = "Invalid PKCS7 Signature."
case invalidPKCS7Type = "Invalid PKCS7 Type."
case invalidAppleRootCertificate = "Public Apple root certificate not found."
case failedAppleSignature = "Receipt not signed by Apple."
case unexpectedASN1Type = "Unexpected ASN1 Type."
case missingComponent = "Expected component was not found."
case invalidBundleIdentifier = "Receipt bundle identifier does not match application bundle identifier."
case invalidVersionIdentifier = "Receipt version identifier does not match application version."
case invalidHash = "Receipt failed hash check."
case invalidExpired = "Receipt has expired."
}
class Receipt {
var receiptStatus: ReceiptStatus?
var bundleIdString: String?
var bundleVersionString: String?
var bundleIdData: Data?
var hashData: Data?
var opaqueData: Data?
var expirationDate: Date?
var receiptCreationDate: Date?
var originalAppVersion: String?
var inAppReceipts: [IAPReceipt] = []
static public func isReceiptPresent() -> Bool {
if let receiptUrl = Bundle.main.appStoreReceiptURL,
let canReach = try? receiptUrl.checkResourceIsReachable(),
canReach {
return true
}
return false
}
}
事实上,在调试器中检查接收变量的状态(如果存在),我可以看到以下内容:
(lldb) p receipt
(MyApp).Receipt?) $R0 = 0x0000000281516ac0 {
receiptStatus = nil
bundleIdString = nil
bundleVersionString = nil
bundleIdData = nil
hashData = nil
opaqueData = nil
expirationDate = nil
receiptCreationDate = nil
originalAppVersion = nil
inAppReceipts = 0 values {}
}
换句话说,它显示充满了nil(s)。
Receipt()
不是一个原生的Swift类,很难从代码中看出发生了什么。在Sandbox中测试收据的一些常见问题是: