Search code examples
iosswiftswift2storekitnserror

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?


Solution

  • 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.