Keep your app's purchase state current
To keep purchase state current with BillDog, subscribe to the customer info stream once and drive your UI from it: BillDog.shared.customerInfoStream on iOS, BillDog.customerInfoStream on Android, BillDog.shared.customerInfoStream in Flutter and BillDog.customerInfoEmitter.addListener in React Native. The SDK refreshes on launch and on foreground, and records store purchases already on the device. Call syncStorePurchases() only when you need to wait for that recording, and restorePurchases() only from a user's tap.
Steps last checked against the SDK source on 16 Nov 2026.
What you'll build
- One listener per platform that unlocks and relocks
premiumas customer info changes - A clear rule for when to use sync, restore and a forced refresh
- Correct behavior across
logInandlogOut
What you'll need
- An app that already configures BillDog and shows a paywall (iOS, Android, Flutter or React Native)
- A product attached to the
premiumentitlement - A sandbox tester or Play license tester to buy with (iOS sandbox, Android sandbox)
1. Know what updates customer info
3 minCustomerInfo is the server's answer to "what does this user have". The SDK caches it per user and publishes every new answer to one stream. You don't need to poll. The stream gets a new value when:
- a purchase or restore finishes, through a paywall or your own code;
- the store reports a transaction while the app runs (on iOS: a renewal, a purchase on another device, an Ask to Buy approval, a refund; on Android: a pending purchase that completes);
- the app launches or returns to the foreground and the cached answer is at least five minutes old;
- you call
getCustomerInfoand it fetches from the server; - the user changes:
logInorlogOut.
Each stream replays its latest value to a new listener, so a screen that subscribes late still gets the current answer straight away.
Refunds and revocations decided on the server (by Apple, Google or you in the dashboard) reach a running app at the next foreground once the cached answer is five minutes old. If a screen must show them sooner, call getCustomerInfo with a forced refresh (step 5).
2. Listen on iOS
5 minIn SwiftUI, BillDog is an ObservableObject and its @Published property customerInfo is written on the main actor. Observe it and the view redraws whenever access changes.
import SwiftUI
import BillDogFull
struct PremiumGate: View {
@ObservedObject private var billDog = BillDog.shared
var body: some View {
if billDog.customerInfo?.hasEntitlement("premium") == true {
Text("Premium unlocked")
} else {
Text("Free plan")
}
}
}Outside a view, iterate customerInfoStream, an AsyncStream<CustomerInfo>. Every iteration gets its own copy of the stream, so several parts of your app can listen at once.
import BillDogFull
@MainActor
final class AccessModel {
static let shared = AccessModel()
private(set) var isPremium = false
private var listener: Task<Void, Never>?
// Call once, after BillDog.shared.configure(config:)
func start() {
guard listener == nil else { return }
listener = Task { [weak self] in
for await info in BillDog.shared.customerInfoStream {
self?.isPremium = info.hasEntitlement("premium")
}
}
}
}If you prefer a delegate, set BillDog.shared.delegate to an object that conforms to BillDogDelegate and implement onCustomerInfoUpdated(customerInfo:). Every method has a default, so implement only the ones you need.
BillDog.shared.delegate is a weak property. Keep a strong reference to your delegate object (for example a property on your App or AppDelegate), or it is released and the callbacks stop silently. The delegate isn't guaranteed to run on the main thread, so hop to the main actor before you touch UI.
3. Listen on Android
5 minBillDog.customerInfoStream is a SharedFlow<CustomerInfo> that replays its latest value. Collect it with the lifecycle so collection stops while the screen is in the background.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import io.billdog.core.BillDog
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// BillDog.configure(...) has already run in your Application.onCreate
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
BillDog.customerInfoStream.collect { info ->
showPremium(info.hasEntitlement("premium"))
}
}
}
}
private fun showPremium(isPremium: Boolean) {
// Update your own UI here
}
}Collect customerInfoStream after BillDog.configure has run. Before that, the property returns an empty placeholder flow that never emits. Configuring in Application.onCreate makes this safe in every Activity.
There is also BillDog.customerInfo, a StateFlow<CustomerInfo?> that is null until the first answer and again right after logOut. And as on iOS, BillDog.delegate takes a BillDogDelegate with onCustomerInfoUpdated(customerInfo). The SDK stores the delegate as a weak reference, so hold your own strong reference to it.
4. Listen in Flutter and React Native
5 minIn Flutter, BillDog.shared.customerInfoStream is a Stream<CustomerInfo>. The native SDK feeds it, so renewals and foreground refreshes arrive here too. Cancel the subscription when the widget goes away.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:billdog_flutter/billdog_flutter.dart';
class PremiumGate extends StatefulWidget {
const PremiumGate({super.key});
@override
State<PremiumGate> createState() => _PremiumGateState();
}
class _PremiumGateState extends State<PremiumGate> {
StreamSubscription<CustomerInfo>? _subscription;
bool _isPremium = false;
@override
void initState() {
super.initState();
_subscription = BillDog.shared.customerInfoStream.listen((info) {
if (mounted) setState(() => _isPremium = info.hasEntitlement('premium'));
});
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) => Text(_isPremium ? 'Premium unlocked' : 'Free plan');
}In React Native and Expo, add a listener to BillDog.customerInfoEmitter. It returns an object with remove(). CustomerInfo.entitlements is an array, so look for an active premium entry.
import { useEffect, useState } from 'react';
import { BillDog, type CustomerInfo } from '@billdog.io/react-native'; // or '@billdog.io/expo'
function hasPremium(info: CustomerInfo): boolean {
return info.entitlements.some((e) => e.identifier === 'premium' && e.isActive);
}
export function usePremium(): boolean {
const [isPremium, setIsPremium] = useState(false);
useEffect(() => {
const subscription = BillDog.customerInfoEmitter.addListener((info) => setIsPremium(hasPremium(info)));
// Seed the listener if nothing has been read yet
BillDog.getCustomerInfo().catch(() => {});
return () => subscription.remove();
}, []);
return isPremium;
}5. Choose between sync, restore and refresh
5 minThree calls look alike but do different jobs. Use the lightest one that answers your question.
- Forced refresh reads the server again and publishes the answer. Use it after something changed outside the app, such as a web checkout or a refund you issued. iOS:
try await BillDog.shared.getCustomerInfo(forceRefresh: true). Android:BillDog.getCustomerInfo(forceRefresh = true). Flutter:BillDog.shared.getCustomerInfo(policy: CacheFetchPolicy.fetchCurrent). React Native:BillDog.getCustomerInfo('FETCH_CURRENT'). - Sync (
syncStorePurchases()) sends BillDog every purchase the store already holds on this device and returns how many the server now has. It never shows a sign-in prompt, so it is safe to call in code. iOS, Android, Flutter and React Native all have it. - Restore asks the store to resync the account and records what it finds. On iOS it calls
AppStore.sync(), which can show the App Store sign-in sheet, so call it only from a Restore Purchases button. iOS and Android:restorePurchases(). Flutter:BillDog.shared.restorePurchases(). React Native:BillDog.restore().
You rarely need to call sync yourself. The iOS and Android SDKs already sweep the device's store purchases when configure runs, again at most every five minutes on foreground, and after every identity change. Call it when you need to wait for the result:
- Existing purchases from before BillDog. Users who bought through your own StoreKit or Play Billing code are recorded by the first sweep after they update. If your first screen must know right away,
awaita sync before reading customer info. - Purchases your own code made. If your app still buys through its own StoreKit or Play Billing code (observer mode), record each purchase with
recordPurchaseand keep sync as the backstop. See Move from raw StoreKit or Play Billing.
import BillDogFull
func refreshAccessAfterUpdate() async throws -> Bool {
// Records what StoreKit holds on this device; never prompts
let held = await BillDog.shared.syncStorePurchases()
print("Server holds \(held) of this device's purchases")
return try await BillDog.shared.getCustomerInfo(forceRefresh: true).hasEntitlement("premium")
}syncPurchases() still exists on iOS and Android as a deprecated alias of syncStorePurchases(). It no longer calls AppStore.sync(). Flutter and React Native have only syncStorePurchases().
6. Handle logIn and logOut
4 minCustomer info belongs to one user, so an identity change resets what your listener sees:
- `logIn(id)` switches to that user's cached answer (or none), fetches theirs from the server and publishes it. It also re-records the device's store purchases for the new user ID. On iOS it returns a
LogInResultwithcustomerInfoandisNewUser; on Android aResult<LogInResult>; in Flutter and React Native it resolves with nothing, so read the user from the stream orgetCustomerInfo. - `logOut()` moves the device to a new anonymous user, clears every cached answer and the stream's replay value, then fetches and publishes the new guest's customer info. On Android,
BillDog.customerInfoisnullin between, and on iOScustomerInfoisnil. Show the locked state until the guest's answer arrives.
No SDK keeps the signed-in user across launches: each launch starts as the install's anonymous user. Call logIn at every launch for a signed-in user, straight after configure.
To check the whole flow, buy with a sandbox account, watch the listener unlock, then refund or let the subscription lapse and bring the app back to the foreground after five minutes. The listener should relock without a restart.