My app crashes with...

NSInvalidArgumentException *** -[__NSCFConstantString stringByAppendingString:]: nil argument

This class of crashes occurs when the Purchases SDK is unable to find certain keys in NSUserDefaults, usually due to clearing NSUserDefaults. The Purchases SDK uses NSUserDefaults to store information. By default, standardUserDefaults are used, but you can configure a different instance of NSUserDefaults. To do this, set the userDefaults parameter in configure:

guard let revenueCatUserDefaults = UserDefaults.init(suiteName: "com.myapp.revenuecat") else {
    fatalError("can't create user defaults for RevenueCat domain")
}
Purchases.configure(withAPIKey: <your-api-key-here>,
                    appUserID: <the-app-user-ID>,
                    observerMode: <observer-mode>,
                    userDefaults: revenueCatUserDefaults)

 

`NSInternalInconsistencyException` after clearing NSUserDefaults

The Purchases SDK uses NSUserDefaults to store information. By default, standardUserDefaults are used, but you can configure a different instance of NSUserDefaults. To do this, set the userDefaults parameter in configure:

guard let revenueCatUserDefaults = UserDefaults.init(suiteName: "com.myapp.revenuecat") else {
    fatalError("can't create user defaults for RevenueCat domain")
}
Purchases.configure(withAPIKey: <your-api-key-here>,
                    appUserID: <the-app-user-ID>,
                    observerMode: <observer-mode>,
                    userDefaults: revenueCatUserDefaults)

 

Take care not to delete the keys used by RevenueCat from userDefaults manually. This will lead to crashing with logs that look like this:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '[Purchases] - Cached appUserID has been deleted from user defaults. This leaves the SDK in an undetermined state. Please make sure that RevenueCat entries in user defaults don't get deleted by anything other than the SDK.'

This crash can also happen if you exceed the NSUserDefaults storage limit. When the NSUserDefaults limit is reached, iOS will delete keys to keep your app's NSUserDefaults below the limit. Although only tvOS has a documented limit, you could hit this on other platforms, see https://github.com/RevenueCat/purchases-ios/issues/225.

Was this article helpful?
0 out of 0 found this helpful