【问题标题】:Use of unresolved identifier when using StoreKit constants with iOS 9.3/Xcode 7.3在 iOS 9.3/Xcode 7.3 中使用 StoreKit 常量时使用未解析的标识符
【发布时间】:2016-07-09 11:59:06
【问题描述】:

我在尝试使用以下 StoreKit 常量之一时收到错误“使用未解析的标识符”:

SKErrorClientInvalid
SKErrorPaymentCancelled
SKErrorPaymentInvalid
SKErrorPaymentNotAllowed
SKErrorStoreProductNotAvailable
SKErrorUnknown

您的代码可能如下所示:

if transaction.error!.code == SKErrorPaymentCancelled {
    print("Transaction Cancelled: \(transaction.error!.localizedDescription)")
}

发生了什么变化?我需要导入新模块吗?

【问题讨论】:

    标签: ios swift swift2 storekit nserror


    【解决方案1】:

    从 iOS 9.3 开始,某些 StoreKit 常量已从 SDK 中删除。有关更改的完整列表,请参阅 StoreKit Changes for Swift

    这些常量已被替换为 SKErrorCode 枚举和相关值:

    SKErrorCode.ClientInvalid
    SKErrorCode.CloudServiceNetworkConnectionFailed
    SKErrorCode.CloudServicePermissionDenied
    SKErrorCode.PaymentCancelled
    SKErrorCode.PaymentInvalid
    SKErrorCode.PaymentNotAllowed
    SKErrorCode.StoreProductNotAvailable
    SKErrorCode.Unknown
    

    您应该使用枚举的rawValue 检查您的transaction.error.code。示例:

    private func failedTransaction(transaction: SKPaymentTransaction) {
        print("failedTransaction...")
        if transaction.error?.code == SKErrorCode.PaymentCancelled.rawValue {
            print("Transaction Cancelled: \(transaction.error?.localizedDescription)")
        }
        else {
            print("Transaction Error: \(transaction.error?.localizedDescription)")
        }
        SKPaymentQueue.defaultQueue().finishTransaction(transaction)
    }
    

    如果在 iOS 9.3 及更高版本上使用 StoreKit 创建新应用程序,您应该检查这些错误代码而不是旧常量。

    【讨论】:

    • “您应该检查是否使用这些值之一检查您的 transaction.error”我认为这是不可能的。 SKErrorCode 不是 NSError。只有针对 .rawValue 的 .code 对我有用。
    • @AceGreen 谢谢,我会进行编辑。所以这对你有用吗? if transaction.error?.code == SKErrorCode.PaymentCancelled.rawValue { ... }
    • 我使用 switch 语句,但是 .code 对 .rawValue 有效(请参阅下面的答案)
    【解决方案2】:

    添加到@JAL 答案是一个开关变体

            switch (transaction.error!.code) {
            case SKErrorCode.Unknown.rawValue:
                print("Unknown error")
                break;
            case SKErrorCode.ClientInvalid.rawValue:
                print("Client Not Allowed To issue Request")
                break;
            case SKErrorCode.PaymentCancelled.rawValue:
                print("User Cancelled Request")
                break;
            case SKErrorCode.PaymentInvalid.rawValue:
                print("Purchase Identifier Invalid")
                break;
            case SKErrorCode.PaymentNotAllowed.rawValue:
                print("Device Not Allowed To Make Payment")
                break;
            default:
                break;
            }
    

    【讨论】:

      【解决方案3】:

      以上答案都不适合我。解决它的方法是将 StoreKit 添加到 SKError。

      我的开关看起来像这样:

      switch (transaction.error!.code) {
              case StoreKit.SKErrorCode.Unknown.rawValue:
                  print("Unknown error")
                  break;
      }
      

      不知道为什么。

      【讨论】:

      • 仅引用 SKErrorCode 结构时遇到的错误是什么?听起来您有命名空间冲突。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      相关资源
      最近更新 更多