SwiftUI Preview Doesn't Work With Revenue Cat

  • 28 October 2021
  • 2 replies
  • 54 views

Badge +1

When my app ran, everything was fine. But with SwiftUI Preview, Revenue Cat can’t run. 

As it relied on the instance of `AppDelegate`, there is no `AppDelegate` in SwiftUI Preview.

```

Purchases.configure(withAPIKey: "api key")

```

Is there a way to set `Purchases.configure(withAPIKey: "api key")` in SwiftUI Preview? Currently I had to comment code so the preview could work.


2 replies

Userlevel 5
Badge +8

Hey @owenzhao!

I’m not very familiar with SwiftUI Preview, but I’m not sure it supports calling AppDelegate initialization code. Since the preview is meant to test/demo your UI, your best bet is likely to run the project on your device so our SDK can make the necessary API calls and go through the proper app lifecycle (as well as making purchases, etc.).

I’ll leave this post open in case anyone else has some context they can add here!

I believe you can do this right within the preview section @owenzhao

i.e.

init(){ Purchases.configure(withAPIKey: "api key") }

Instead of having to provide this for each screen however I recommend a class for RC which sets this all up. In my preview class for example I just do something like this:

SubscriptionView()
            .environmentObject(SubscriptionManager())

The init of the manager handles the Purchases.configure

It also helps to run the preview in live mode so that the callback for offerings is rendered as well.

Reply