Question

How to initialize the RevenueCat SDK in an iOS Widget

  • 7 August 2023
  • 2 replies
  • 149 views

Badge +5

I want to use the RevenueCat SDK in my iOS widgets. I tried to use it once before but our users were reporting that their widgets would randomly show “no subscription” (our UI when we determine the user doesn’t have a current subscription), so I commented out the RevenueCat specific code but now I want to try to add it back in.

 

The SDK notes say (https://www.revenuecat.com/docs/configuring-sdk):

“You should only configure Purchases once, usually early in your application lifecycle. After configuration, the same instance is shared throughout your app by accessing the .shared instance in the SDK.”

 

However there doesn’t seem to be a good way to achieve this inside an iOS widget since getTimeline() can be called multiple times, I didn’t see another place where you can easily instantiate a singleton RevenueCat instance. I’m not sure if this multiple Purchases object issue was the cause of our previous issues where sometimes it was reported the user didn’t have a subscription even when they did, or if it was something else.

 

Is there some example code of where/how to safely use the RevenueCat SDK inside an iOS widget. We were using the 4.13.2 version of the SDK before but I just updated to the 4.25.2 latest version, so maybe that might fix it as well, not sure.

 

We were already using the shared app group for the widget before as documented in this link: https://www.revenuecat.com/docs/ios-app-extensions

 

Thanks

Mark.


2 replies

Userlevel 4
Badge +6

Hey @mrd,

While some example code for a widget implementing RevenueCat is on our to-do list, we don’t have anything available at this time. 

In order to set up an instance of configure in your widget (in addition to your main app), I believe there's an entry point where you can configure the framework, such as an init.

An alternate approach could be to only configure RevenueCat in the main app, then to share subscription status manually through your own shared UserDefaults.

Badge +2

Hey @mrd,

While some example code for a widget implementing RevenueCat is on our to-do list, we don’t have anything available at this time. 

In order to set up an instance of configure in your widget (in addition to your main app), I believe there's an entry point where you can configure the framework, such as an init.

An alternate approach could be to only configure RevenueCat in the main app, then to share subscription status manually through your own shared UserDefaults.

 I’d also be interested in some best practices code from RevenueCat for widgets. Your suggestion to configure RC in the main app and then share status through UserDefaults would result in risk of service theft. The user could let their subscription lapse, and as long as they don’t launch the app again the widget would remain authenticated and continue to consume (in my case) expensive API calls to my data service provider. RevenueCat’s documentation has always said to directly check the entitlements rather than caching it to avoid this type of problem.

I’m not aware of a centralized entry point into widgetkit execution. Is it acceptable to call Purchases.configure() every time the TimelineProvider fires for each widget?

 

 

Reply