0

I am seeing examples of code using SKError "code" property or variable. I cannot seem to define any definition for this property. How is xcode / Swift able to access this property? I am using SwiftyStoreKit with this issue. Thanks

Tried looking in the documentation for SKError, nothing there. Tried debugging in xcode, live, cannot understand from xcode where is this thing coming from.

    SwiftyStoreKit.purchaseProduct("X", quantity: 1, atomically: true) { result in
        switch result {
        case .success(let purchase):
            print("Purchase Success: \(purchase.productId)")
            let defaults = UserDefaults.standard
            defaults.set(true, forKey: "Y")
        case .error(let error):
            switch error.code {
            case .unknown: self.Alert("Z")

Just want to know how xcode can access this property.

roywax
  • 63
  • 1
  • 8
  • 1
    "Tried looking in the documentation for SKError, nothing there" It is _right_ there.https://developer.apple.com/documentation/storekit/skerror – matt May 30 '19 at 21:45

2 Answers2

1

See SKError.Code documentation, found in the SKError documentation. In Xcode you can also press shift+command+o (the letter “oh”) and type SKError and jump right to the definition.


These are the values at this point:

case unknown
Error code indicating that an unknown or unexpected error occurred.

case clientInvalid
Error code indicating that the client is not allowed to perform the attempted action.

case paymentCancelled
Error code indicating that the user canceled a payment request.

case paymentInvalid
Error code indicating that one of the payment parameters was not recognized by the App Store.

case paymentNotAllowed
Error code indicating that the user is not allowed to authorize payments.

case storeProductNotAvailable
Error code indicating that the requested product is not available in the store.

case cloudServicePermissionDenied
Error code indicating that the user has not allowed access to Cloud service information.

case cloudServiceNetworkConnectionFailed
Error code indicating that the device could not connect to the network.

case cloudServiceRevoked
Error code indicating that the user has revoked permission to use this cloud service.

case privacyAcknowledgementRequired
Error code indicating that the user has not yet acknowledged Apple’s privacy policy for Apple Music.

case unauthorizedRequestData
Error code indicating that the app is attempting to use a property for which it does not have the required entitlement.

case invalidOfferIdentifier
Error code indicating that the offer identifier is invalid.

case invalidOfferPrice
Error code indicating that the price you specified in App Store Connect is no longer valid.

case invalidSignature
Error code indicating that the signature in a payment discount is not valid.

case missingOfferParams
Error code indicating that parameters are missing in a payment discount.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • But what are the values for that enum, does it start from 0? – alx May 31 '21 at 22:25
  • The whole idea of using enums is that you shouldn't be checking for numeric values at all, but rather switching on the enumeration keys. But if you really want, you can just print some of the values and see what their respective raw/underlying values are. – Rob May 31 '21 at 22:45
0

It may help to realize that SKError conforms to Error, which is bridged to NSError. If you look at the NSError docs you'll see that it has a code property which is an Int. Therefore SKError has this too.

In the Objective-C world, from which all this comes ultimately, the possible NSInteger values of an SKError's code are listed in an Objective-C enum:

typedef NS_ENUM(NSInteger,SKErrorCode) {
    SKErrorUnknown,
    SKErrorClientInvalid,
    SKErrorPaymentCancelled, 
    // ...
}

That is imported into Swift as a Swift enum with an Int raw value, of type SKError.Code:

public struct SKError {
    public enum Code : Int {
        case unknown
        case clientInvalid
        case paymentCancelled
        // ...
    }
}

Thus as a convenience the names .clientInvalid and so forth can be used in comparison with an SKError's code.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • My problem is that all these "bridging" is done implicitly and is not shown in Xcode in an understand way - like go to definition. I have seen other things like that was problematic for me to understand. In any case I now understand the issue more clearly thanks to everybody's help. – roywax Jun 14 '19 at 19:56