在应用内购买(验证)中,收货回执状态为零

时间:2019-03-28 04:51:55

标签: ios in-app-purchase receipt-validation

我正在尝试在iOS应用中实施“应用内购买收据”验证。 我遵循了this tutorial,并使用提供的示例项目作为开发自己的应用程序的基础。

这是我目前面临的一个问题。在名为validateReceipt()的函数中:

  func validateReceipt() {
    ......

    receipt = Receipt()

    if let receiptStatus = receipt?.receiptStatus { // Critical point.
        ...... // A lot of important things happening here.
    }
    ......
  }

在调试器中发生什么之后,我看到到达注释行: // Critical point 。我有,不是收到空,而是:

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 {}
}

换句话说,它显示为零。

1 个答案:

答案 0 :(得分:0)

SharedPreferences不是本机Swift类,因此很难从该代码中分辨出发生了什么。沙盒中的测试收据存在一些常见问题:

  1. 需要在物理设备上
  2. 在沙盒中安装后需要已经购买才能有收据。在生产环境中,安装后设备上将有一张收据,在沙盒中,直到购买后才创建收据。