简体   繁体   中英

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

I get the error "Use of unresolved identifier" when trying to use one of these StoreKit constants:

SKErrorClientInvalid
SKErrorPaymentCancelled
SKErrorPaymentInvalid
SKErrorPaymentNotAllowed
SKErrorStoreProductNotAvailable
SKErrorUnknown

Your code may look like this:

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

What changed? Is there a new module I need to import?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM