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

点点圈 提交于 2019-11-30 20:18:15

As of iOS 9.3 certain StoreKit constants have been removed from the SDK. See StoreKit Changes for Swift for the full list of changes.

These constants have been replaced in favor of the SKErrorCode enum and associated values:

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

You should check be checking your transaction.error.code with the enum's rawValue. Example:

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

You should be checking against these error codes rather than the legacy constants if creating a new application using StoreKit on iOS 9.3 and above.

Adding to @JAL answer heres a switch variant

        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 of the above answers worked for me. What solved it was prepending StoreKit to the SKError.

My switch looked something like this:

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

No idea why.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!