Skip to main content
Question

How to initialize the RevenueCat SDK in an iOS Widget


Forum|alt.badge.img+6
  • Helper
  • 9 replies

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.

5 replies

kaitlin
RevenueCat Staff
Forum|alt.badge.img+6
  • RevenueCat Staff
  • 342 replies
  • August 9, 2023

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.


Forum|alt.badge.img+2
  • New Member
  • 4 replies
  • September 27, 2023
kaitlin wrote:

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?

 

 


Forum|alt.badge.img+1
  • Helper
  • 3 replies
  • December 20, 2024

@RKH ​@mrd ​@kaitlin 

I am in the same situation as you. We are developing a keyboard extension app based on auto-renewable subscriptions using RevenueCat version 5.12. However, simply updating `customerInfo` in the Containing App and storing the subscription status in `UserDefaults(appGroupID:)` does not provide a way for the keyboard extension to determine if a user's subscription has expired until the user open again the Containing App.

As a result, we have no choice but to call `Purchases.configure()` every time our custom keyboard's `viewDidLoad` is triggered. Have you found a way to share the main app's `Purchases.configure()` with the extension?

Your solution would be incredibly helpful to us.


Forum|alt.badge.img+2
  • New Member
  • 4 replies
  • December 21, 2024

@bg_kim 

I am using UserDefaults, and I don’t like it.

I found that for widgets I could run Purchases.configure() in my timeline provider class, and it seemed to work perfectly in testing. But it was my top crash source in the deployed app, according to Xcode Organizer. I have no idea what was causing the crash and never replicated it, but it was a common enough crash that I went back to UserDefaults. Most of my app’s functionality is in the app, not the widget, so I felt like I could tolerate the risk. In your case, it seems to not be viable since the user could use the keyboard extension forever and never open the app again.

I’ve never looked at how keyboard extensions work and what the lifecycle is. Widgets are aggressively managed by iOS and my guess is that I was seeing some interaction with the memory or CPU constraints that was hitting my app widget init. 

I’d love to get some best practice code from RC on this!


Forum|alt.badge.img+1
  • Helper
  • 3 replies
  • December 21, 2024

@RKH 

Thank you for explaining your solution in detail. At the moment, we also feel that we have no choice but to use UserDefaults. However, as you mentioned, we are concerned about the risk of users only using the keyboard extension and never opening the main app. This concern also includes whether we might have to shift our business model from a subscription-based model to a one-time purchase model. We hope that RevenueCat will come up with a good solution for these challenges related to extension apps soon.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings