如何在生产中获得原始应用程序版本?

Bra*_*ogs 5 ios receipt-validation swift

我们最近将可购买的应用程序转换为“免费增值”模式。我们正在使用 Bundle.main.appStoreReceiptURL 来拉取收据,然后检查“original_application_version”以查看用户是否从 App Store 下载了较旧的付费版本,或者他们是否下载了较新的免费版本,并且在应用内购买了非消耗品升级到完整版。

这在测试 Sandbox 时非常有效,但在生产中,旧版本的应用程序无法正确验证它们是否在免费增值版本之前下载。

以下代码使用 productionStoreURL 和从 Bundle.main.appStoreReceiptURL 获得的收据调用:

private let productionStoreURL = URL(string: "https://buy.itunes.apple.com/verifyReceipt")
private let sandboxStoreURL = URL(string: "https://sandbox.itunes.apple.com/verifyReceipt")

private func verifyIfPurchasedBeforeFreemium(_ storeURL: URL, _ receipt: Data) {
    do {
        let requestContents:Dictionary = ["receipt-data": receipt.base64EncodedString()]
        let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: [])

        var storeRequest = URLRequest(url: storeURL)
        storeRequest.httpMethod = "POST"
        storeRequest.httpBody = requestData

        URLSession.shared.dataTask(with: storeRequest) { (data, response, error) in
            DispatchQueue.main.async {
                if data != nil {
                    do {
                        let jsonResponse = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any?]

                        if let statusCode = jsonResponse["status"] as? Int {
                            if statusCode == 21007 {
                                print("Switching to test against sandbox")
                                self.verifyIfPurchasedBeforeFreemium(self.sandboxStoreURL!, receipt)
                            }
                        }

                        if let receiptResponse = jsonResponse["receipt"] as? [String: Any?], let originalVersion = receiptResponse["original_application_version"] as? String {
                            if self.isPaidVersionNumber(originalVersion) {
                                // Update to full paid version of app
                                UserDefaults.standard.set(true, forKey: upgradeKeys.isUpgraded)
                                NotificationCenter.default.post(name: .UpgradedVersionNotification, object: nil)
                            }
                        }
                    } catch {
                        print("Error: " + error.localizedDescription)
                    }
                }
            }
            }.resume()
    } catch {
        print("Error: " + error.localizedDescription)
    }
}

private func isPaidVersionNumber(_ originalVersion: String) -> Bool {
    let pattern:String = "^\\d+\\.\\d+"
    do {
        let regex = try NSRegularExpression(pattern: pattern, options: [])
        let results = regex.matches(in: originalVersion, options: [], range: NSMakeRange(0, originalVersion.count))

        let original = results.map {
            Double(originalVersion[Range($0.range, in: originalVersion)!])
        }

        if original.count > 0, original[0]! < firstFreemiumVersion {
            print("App purchased prior to Freemium model")
            return true
        }
    } catch {
        print("Paid Version RegEx Error.")
    }
    return false
}
Run Code Online (Sandbox Code Playgroud)

第一个免费增值版本是 3.2,这是我们当前的版本。所有以前的版本都是 3.1.6 或更早版本。

生产 URL 不应该是问题,否则它不会回退 21007 状态代码来触发我们的沙盒验证。但是,对此进行故障排除特别棘手,因为我们无法针对 Apple 的生产 URL 本身进行测试。

有没有人知道为什么这会在 Sandbox 中工作而不是在生产中工作?

Bra*_*ogs 5

看起来获取收据根本不是问题。

的一些旧值的original_application_version格式不正确,导致我们无法获取要比较的应用程序版本。

  • @Mr.Zystem 如果您使用的是 Git 或其他版本控制系统,您可以查看 **plist.info** 文件的历史记录。`CFBundleVersion` 值是 XCode 中的内部版本号,是作为 `original_application_version` 返回的值。 (3认同)