Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 620
JAL Avatar asked Mar 22 '16 14:03

JAL


2 Answers

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.

like image 140
JAL Avatar answered Sep 30 '22 02:09

JAL


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;
        }
like image 21
Ace Green Avatar answered Sep 30 '22 03:09

Ace Green