对于尚未在 Apple App Store 上发布的免费应用程序,我正在努力测试收据验证功能。
Bundle.main.appStoreReceiptURL
是收据明显所在的位置,但不能保证收据末尾有文件。
我的假设是:
- 从 App Store 下载应用程序后,必须有收据。即使应用程序是免费的
- 此收据可通过以下方式验证:https ://developer.apple.com/documentation/appstorereceipts/responsebody/receipt
- 创建该收据不一定需要应用内购买。
上述假设是否准确?如果是这样,甚至无法找到查看样本收据的方法(即使在 TestFlight 上下载)。
我不知道如何测试这个!
extension DebugViewController: SKRequestDelegate {
func checkReceipt() {
let msg: String
let url = Bundle.main.appStoreReceiptURL!
if FileManager.default.fileExists(atPath: url.path) {
if let data = try? Data(contentsOf: url) {
msg = data.description
print(msg)
let t = TextViewController(msg)
show(t, sender: nil)
}
}
else {
msg = "No file Found at the url, requesting from appstore...\n\n\(url.absoluteString)"
let request = SKReceiptRefreshRequest(receiptProperties: nil)
request.delegate = self
self.request = request
self.request?.start()
}
}
func requestDidFinish(_ request: SKRequest) {
showMsg("completed request", "completed")
var msg: String = "Starting.."
let url = Bundle.main.appStoreReceiptURL!
if FileManager.default.fileExists(atPath: url.path) {
if let data = try? Data(contentsOf: url) {
msg = data.description
print(msg)
let t = TextViewController(msg)
show(t, sender: nil)
}
}
else {
msg = "No file exists at path \(url.absoluteString) despite refreshing"
let t = TextViewController(msg)
show(t, sender: nil)
}
}
func request(_ request: SKRequest, didFailWithError error: Error) {
let errorStr = String(describing: error)
let t = TextViewController(errorStr)
show(t, sender: nil)
}
}