These error codes, known as SKError codes, provide information on why a specific In-App Purchase payment request failed. In this article, we'll take a look at each of the SKError codes and provide guidance on how to handle them.
In the context of the StoreKit framework, NSUnderlyingError
is an error object that can be returned as part of a SKError
object. When an in-app purchase request fails, especially with the code SKErrorUnknown
, the SKError
object may include an NSUnderlyingError
object that provides additional details about the cause of the error.
"error": { "code": "E_UNKNOWN", "message": "An unknown error occurred", "domain": "SKErrorDomain", "userInfo": { "NSUnderlyingError": { "code": "504", "message": "underlying error", "domain": "ASDErrorDomain", "userInfo": { "NSUnderlyingError": { "code": "305", "message": "underlying error", "domain": "AMSErrorDomain", "userInfo": { ... "NSLocalizedDescription": "Server Error", "NSLocalizedFailureReason": "Verification Required", } ...
In the code example above, you can find the localized failure reason: 'Verification Required', which is actually the real reason for the payment failure. The user has probably been prompted to verify their payment method by the system.
Note that this particular example has two levels of underlying errors: one with code 504 and another with code 305. You should always search deeply to find the last underlying error and get its failure reason.
To access the underlying error object in this scenario, you would first check the code
property of the SKError
object to determine that it is a SKErrorUnknown
. You could then call the userInfo
method on the SKError
object to retrieve any additional information, including the NSUnderlyingError
object.
This is a basic example of how to get the localized failure reason of the SKError
code for iOS in-app purchases:
extension NSError { var underlyingErrorDescription: String? { if let error = userInfo["NSUnderlyingError"] as? NSError { if let innerError = error.userInfo["NSUnderlyingError"] as? NSError { return innerError.localizedFailureReason } return error.localizedFailureReason } return nil } }
Handling NSUnderlyingError
objects in the context of StoreKit can be important for diagnosing and resolving issues related to in-app purchases. By accessing the underlying error object, you can gain more insight into the root cause of the error and provide more helpful guidance on how to resolve the issue.
In conclusion, being familiar with the SKError
codes is an essential part of developing an iOS app that includes in-app purchases. By understanding each error code and how to handle it, you can provide a better user experience and ensure that your app is successful.
The Apphud SDK automatically handles necessary error codes. With Apphud, you won't miss any successful purchases, even if the purchase has been completed outside the app after the payment verification has been updated. Sign up for Apphud today and let us provide you with subscription infrastructure.