Is it possible to only offer a certain amount of widget functionality to users who have purchased an in-app purchase or are ongoing subscribers?
Thanks!
Yes it's possible to paywall a widget. What I did was:
Highly simplified example:
// Actual widget view
struct WidgetView: View {
var entry: WidgetEntry
var body: some View {
VStack(alignment: .leading) {
Spacer()
Text(entry.widgetText)
.font(.system(size: 12, weight: .semibold, design: .default))
.padding(.leading, 16)
.padding(.trailing, 16)
.padding(.bottom, 16)
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.white)
}
}
}
// Paywall widget view
struct WidgetPaywallView: View {
var body: some View {
VStack(alignment: .leading) {
Spacer()
Text("Subscribe to use the widget")
.font(.system(size: 12, weight: .semibold, design: .default))
.padding(.leading, 16)
.padding(.trailing, 16)
.padding(.bottom, 16)
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.white)
}
}
}
struct WidgetEntry: TimelineEntry {
let date: Date
let widgetText: String
var isPremiumSubscriber: Bool
}
Note that with this approach, if the user's subscription status lapses midway through an already created timeline, then the user will still have widget functionality until the timeline runs out and getTimeline() is called again.
struct WidgetEntryView: View {
var entry: WidgetEntry
var body: some View {
// Display widget if user is subscribed, otherwise return the paywall view
if entry.isPremiumSubscriber {
WidgetView(entry: entry)
} else {
WidgetPaywallView()
}
}
}
var isPremiumSubscriber: Bool = false {
didSet {
if #available(iOS 14.0, *) {
if isPremiumSubscriber != oldValue {
// Update widgets because subscription status has changed
WidgetCenter.shared.reloadAllTimelines()
}
}
}
}
Great explanation but I don't get how do you check with RevenueCat if the premium is active in the getTimeline() method. Can you explain that? Thank you!
Would also love more of an explanation on the logic in getTimeline(). I continue to get an error when trying to work with the SDK from within the extension that no such module exists.
You can check and write current premium status to appgroup's user defaults from the app, and then check it in the widget (from the same appgroup)