A StoreKit 2 one-time unlock with no server: five edge cases
Purrchiver is free to download, with one non-consumable in-app purchase that unlocks the advanced features. There’s no server and no network entitlement: StoreKit 2 verifies transactions on the device, and the app’s own checks are local. That keeps the privacy story simple, but it means every edge case of the purchase flow has to be handled correctly on the Mac, with nothing upstream to reconcile it later.
This week, while getting the unlock ready to ship, testing and an audit of the StoreKit code turned up five problems, all in paths a simple “buy, then check it’s unlocked” test doesn’t exercise. Four were bugs. The fifth was a correct behavior with nothing protecting it.
1. The first Buy tap didn’t unlock
Symptom: product.purchase() returned .success, but the app stayed on
the paywall. A second tap on Buy worked.
Cause: on success, the code re-derived “do I own it?” by iterating
Transaction.currentEntitlements. The transaction I’d just been handed hadn’t
propagated into currentEntitlements yet, so the scan said “not owned”.
Fix: grant from the verified transaction that purchase() returned, then
finish it:
case .success(.verified(let tx)):
if grantsAccess(tx) { isUnlocked = true }
await tx.finish()
case .success(.unverified):
await reconcileFromCurrentEntitlements()currentEntitlements is still the source of truth everywhere else, including
for refunds. It’s just not reliable in the instant after a purchase.
Nothing caught this earlier because no test drove the app’s own purchase()
to .success. The “owned” test bought through SKTestSession directly and
then checked state, which skips the code path that was wrong. There’s now a
test that calls purchase() once and expects the app to be unlocked.
The same change fixed a quieter problem: if the product failed to load,
purchase() silently returned and the Buy button did nothing. It now throws,
so the paywall can show an error.
2. Unfinished transactions replayed every launch
Symptom: none visible, which is the problem.
Cause: Transaction.unfinished had no call sites. Any transaction
StoreKit still considered unfinished, whether owned or revoked, was delivered
again on every cold launch and stayed in the queue.
Fix: drain it at launch, before the first entitlement scan:
for await result in Transaction.unfinished {
await handle(result) // re-derive state, then tx.finish()
}
isUnlocked = await currentlyOwnsUnlock()The handler finishes the transaction even when it’s revoked or unverified.
Skipping finish() for a transaction you’ve decided not to trust leaves it in
the queue forever.
3. Restore could lock out a paying user
Symptom: a user who owned the unlock taps Restore Purchase and ends up locked.
Cause: restore() called AppStore.sync(), scanned
currentEntitlements, and set isUnlocked to whatever the scan found. A scan
that transiently comes back empty after a successful sync set it to false.
Fix: Restore is additive. It can grant; it never revokes.
try await AppStore.sync()
if await currentlyOwnsUnlock() {
isUnlocked = true
}Re-locking on a real refund is someone else’s job (see the next section). Restore exists for people who should have access and don’t, and App Review expects it to work for them.
The Restore button in Settings had its own bug. It ran
Task { try? await restore() }, which discarded both the result and any
error, so a restore that found nothing looked the same as one that worked.
It now shows the outcome inline and announces it for VoiceOver.
4. A refund didn’t re-lock until relaunch
Symptom: refund the purchase while the app is running and the features stay unlocked until the next launch.
Cause: the Transaction.updates listener only accepted verified,
non-revoked transactions. A refund arrives as a revoked transaction, so the
listener dropped it and changed nothing.
Fix: the listener now treats every update as a signal to re-check, not as
the answer. It finishes the transaction whatever its state, then re-derives
from currentEntitlements, which excludes revoked purchases:
for await result in Transaction.updates {
let tx: Transaction
switch result {
case .verified(let t), .unverified(let t, _): tx = t
}
let wasUnlocked = isUnlocked
isUnlocked = await currentlyOwnsUnlock()
if wasUnlocked && !isUnlocked { didLoseUnlockMidSession = true }
await tx.finish()
}The transaction pulled out of an .unverified result is used only to call
finish(). Whether the app is unlocked comes only from the
currentEntitlements scan, which accepts verified, non-revoked transactions.
Silently re-locking features mid-session is its own problem: something stops working and nothing says why. The flag drives a one-time alert, shown in one window, that says what happened and where to buy again.
Update, October 23: Two parts of this changed for 1.1. currentEntitlements
can lag behind a delivered transaction in either direction, so a purchase, an
Ask to Buy approval or a Family Sharing grant could stay locked for a while and
a refund could stay unlocked. Now a verified, non-revoked unlock transaction
that arrives on Transaction.updates unlocks at once. Otherwise the other
unlock transactions in currentEntitlements decide, leaving out the delivered
one if it was revoked, since right after a refund it can still be listed there.
And the alert belonged to an archive window, so with only the welcome window
open it never appeared. It’s now app-wide, shown once on the window in front,
and a refund made while the app was quit is explained at the next launch.
Testing this against SKTestSession needs a bounded poll, because
refundTransaction returns before currentEntitlements reflects the refund.
5. Family Sharing worked by accident
Family Sharing is turned on for the unlock, so a family member’s copy has to work. It did. It was also one line away from breaking with no test to notice.
Family-shared owners got the unlock only because the ownership check never
looked at Transaction.ownershipType. Anyone tidying that check up by adding
ownershipType == .purchased would have silently locked out every
family-shared owner.
SKTestSession can’t create a .familyShared transaction, so I couldn’t test
it end to end. Instead I pulled the per-transaction decision into a pure
function that takes ownershipType as a parameter and deliberately ignores
it, and tested the function directly:
@Test func familySharedGrantsAccess() {
#expect(grantsAccess(isVerified: true, revoked: false,
ownershipType: .familyShared))
}Next to it are tests that unverified, revoked, and wrong-product transactions
don’t grant. The function is what both the purchase path and the
currentEntitlements scan call, so the test covers the real decision.
Bonus: the test config was in the app bundle
The local Products.storekit file used by the tests was a member of the app
target, so it shipped inside Contents/Resources. It did nothing there,
because the shipping app uses the live App Store, but it had no reason to be
there either. It’s now a member of the test target only, where
SKTestSession(configurationFileNamed:) still finds it.
What to take away
- Grant from the purchase result; reconcile from
currentEntitlements. - Drain
Transaction.unfinishedat launch, and finish every transaction, including the ones you reject. - Make Restore grant-only.
- Let
Transaction.updatestrigger a re-check rather than decide the answer itself. (Update, October 23: except that a verified grant it delivers now unlocks at once; see section 4.) - Write a test for any invariant that holds only because a line of code isn’t there.