gpt4 book ai didi

swift - 收据验证 iOS - 文件无法打开,因为没有这样的文件

转载 作者:行者123 更新时间:2023-11-28 15:41:39 25 4
gpt4 key购买 nike

我正在开发订阅 IAP。我设置了所有购买,他们的详细信息很好地返回,我可以在沙盒中进行购买并获得所有消息。我现在遇到的问题是检查收据。我总是能正常返回一个 URL,但是当我尝试读取它时,我不断收到错误消息,提示该文件不存在。所以我尝试使用 SKReceiptRefreshRequest 进行刷新。再试一次,还是一样。

我已经在模拟器和两个真实设备上卸载了应用程序,从新安装和同样的问题重试。我意识到的一件事是,其中一台真实设备显示带有 [Sandbox] 提及的密码提示请求。但是,在两次提示(包括接受密码)之后,我没有完成购买,而是收到一条“用户/密码不匹配”消息。在模拟器上,当提示输入 iTunes 帐户和密码时,一切都通过了,但实际的购买确认从未到来(我等了 4 分钟,互联网连接稳定)。

这是验证过程(我改了很多次,来自不同的教程和其他人的问题)

let receiptURL = Bundle.main.appStoreReceiptURL
func receiptValidation() {
print("1")
print("2", receiptURL)
do {
print("3")
let receiptData = try Data(contentsOf: receiptURL!, options: .alwaysMapped)
print(receiptData)
let receiptString = receiptData.base64EncodedString(options: [])
let dict = ["receipt-data" : receiptString, "password" : "\(password)"] as [String : Any]

do {
print("4")
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

if let sandboxURL = Foundation.URL(string:"https://sandbox.itunes.apple.com/verifyReceipt") {
print("5")
var request = URLRequest(url: sandboxURL)
request.httpMethod = "POST"
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { data, response, error in
print("6")
if let receivedData = data,
let httpResponse = response as? HTTPURLResponse,
error == nil,
httpResponse.statusCode == 200 {
print("7")
do {
print("8")
if let jsonResponse = try JSONSerialization.jsonObject(with: receivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String, AnyObject> {
print(jsonResponse, jsonResponse.count)
// parse and verify the required informatin in the jsonResponse

} else { print("Failed to cast serialized JSON to Dictionary<String, AnyObject>") }
}
catch { print("Couldn't serialize JSON with error: " + error.localizedDescription) }
}
}
print("51")
task.resume()
} else { print("Couldn't convert string into URL. Check for special characters.") }
}
catch { print("Couldn't create JSON with error: " + error.localizedDescription) }
}
catch {
let appReceiptRefreshRequest = SKReceiptRefreshRequest(receiptProperties: nil)
appReceiptRefreshRequest.delegate = self
appReceiptRefreshRequest.start()
print("Couldn't read receipt data with error: " + error.localizedDescription) }
}

func requestDidFinish(_ request: SKRequest) {
print("???")
do {
let receipt = try Data(contentsOf: receiptURL!) //force unwrap is safe here, control can't land here if receiptURL is nil
print(receipt)
} catch {
print("WTF NO RECEIPT")
// still no receipt, possible but unlikely to occur since this is the "success" delegate method
}
}

这是运行应用程序的调试输出。 receiptURL 因模拟器/真实设备而异,但除此之外一切都保持不变。

1
2 Optional(file:///Users/apple/Library/Developer/CoreSimulator/Devices/47EA3293-9B13-4808-BD0B-13D884D14BFE/data/Containers/Data/Application/2F1B7E4E-C523-4270-BF46-6D77F7A2220C/StoreKit/receipt)
3
Couldn't read receipt data with error: The file “receipt” couldn’t be opened because there is no such file.
???
WTF NO RECEIPT
???
WTF NO RECEIPT

为什么我无法创建或找到收据?是设备问题、错误还是我忘记了什么?

最佳答案

整个 IAP 过程异步进行,因此除非整个过程成功完成,否则您不会收到收据数据。根据您在上面的问题中粘贴的内容,我看不到完整的代码,但是如果您尝试在按钮或类似操作的操作上立即访问收据数据,您将不会得到它。

访问收据数据的正确方法是尝试根据 IAP 请求的成功完成处理程序回调来访问收据。提交 IAP 请求后,将有一个服务器端进程负责处理 IAP,然后触发来自 IAP SKPaymentTransactionObserver 类的回调处理程序。使用此类中的通知处理程序,您可以将更新发送到您的 ViewController 以检查收据数据。

关于swift - 收据验证 iOS - 文件无法打开,因为没有这样的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com