在 iOS 9.3/Xcode 7.3 中使用 StoreKit 常量时使用未解析的标识符

Use of unresolved identifier when using StoreKit constants with iOS 9.3/Xcode 7.3

我在尝试使用以下 StoreKit 常量之一时收到错误 "Use of unresolved identifier":

SKErrorClientInvalid
SKErrorPaymentCancelled
SKErrorPaymentInvalid
SKErrorPaymentNotAllowed
SKErrorStoreProductNotAvailable
SKErrorUnknown

您的代码可能如下所示:

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

发生了什么变化?是否有我需要导入的新模块?

从 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 创建新应用程序,您应该检查这些错误代码而不是遗留常量。

添加到@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;
        }

None 以上答案对我有用。解决它的方法是将 StoreKit 添加到 SKError。

我的开关看起来像这样:

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

不知道为什么。