iosstorekit

SKPaymentQueue error code 4097 when trying to purchase in-app product


I am using StoreKit to handle in-app purchases from my iOS app. The purchases usually succeed, but there is this strange error that I am trying to figure out what can be done with it:

<SKPaymentQueue: 0x281d5ccc0>: Error in remote proxy while processing transaction: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service with pid 60667 named com.apple.storekitd" UserInfo={NSDebugDescription=connection to service with pid 60667 named com.apple.storekitd}

The error happens rarely, but it is common enough to be a concern for my app. I am initializing the products first using SKProductsRequest and after the initialization completes the product may be purchased using SKMutablePayment.

I was not able to find a helpful documentation regarding this error. Why is it caused? Is there a way to prevent it?


Solution

  • An error with the NSCocoaErrorDomain domain and the code 4097 can be returned whenever an associated helper daemon process has crashed or doesn't respond as it should. I've seen this with CloudKit code when the "cloudd" process has failed. As you've shown it can happen with StoreKit code if the "com.apple.storekitd" process is having issues.

    In many cases the solution is to retry the operation after a delay. This gives the offending process a chance to get restarted by iOS.

    Wherever you get the Error, you can add code similar to the following:

    if let nserror = error as? NSError, nserror.domain == NSCocoaErrorDomain && nserror.code == 4097 {
        // Wait 10-30 seconds and try the transaction again
    }
    

    where error is an optional Error property or variable.

    In some rare cases the problem could require a device reboot. It's not the fault of your app. It's an iOS issue.