Skip to content

Commit

Permalink
Document .hasPurchased and .appDeviceToken via a new FAQ entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Jul 25, 2024
1 parent a624a70 commit 652ea74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
61 changes: 61 additions & 0 deletions Sources/FreemiumKit/FreemiumKit.docc/FAQ/FAQ-ServerLimits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# How can I apply my custom server-side limits?

Learn how you can recognize a purchased user across app installs and devices to roll your own server-side logic for custom limits.

@Metadata {
@TitleHeading("FAQs")
@PageKind(sampleCode)
}

## Short Answer

FreemiumKit uses the built-in `appDeviceToken` in StoreKit 2 to recognize the same user across app installs and devices. You can access it from the `FreemiumKit` instance either using the `@EnvironmentObject` method or globally via `FreemiumKit.shared.appDeviceToken`.

## Full Answer

If you have your own server-side logic for locking/unlocking functionality based on usage (such as '100 posts per month'), you will want to identify the same user across app installs or devices. To do this, you can access `appAccountToken` on the `FreemiumKit` environment object which will return a `UUID` stored right within StoreKit (so Apple makes sure you recognize the same user across devices – we don't that data on our servers).

Access the field only after purchases are loaded like so:

```swift
import FreemiumKit

struct MyView: View {
@EnvironmentObject private var freemiumKit: FreemiumKit

var body: some View {
VStack {
// your main view ...
}
.onAppear {
if freemiumkit.purchasesLoaded {
let appAccountToken: UUID = freemiumKit.appAccountToken
// do something with the app account token
}
}
.onChange(of: freemiumKit.purchasesLoaded) {
if freemiumkit.purchasesLoaded {
let appAccountToken: UUID = freemiumKit.appAccountToken
// do something with the app account token
}
}
}
}
```

You can also access it globally via `FreemiumKit.shared.appAccountToken` but make sure that `FreemiumKit.shared.purchasesLoad` is `true`, else you won't get the correct `UUID`. Note that FreemiumKit loads this data upon app start and caches it, so most of the time it should be available.


## Contact

Have questions or need support? Reach out to me at [[email protected]](mailto:[email protected]).

---

## Legal

@Small {
Cihat Gündüz © 2024. All rights reserved.
Privacy: No personal data is tracked on this site.
[Imprint](https://www.fline.dev/imprint/)
}
1 change: 1 addition & 0 deletions Sources/FreemiumKit/FreemiumKit.docc/FAQs.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The most frequently asked questions about FreemiumKit.
- <doc:FAQ-Validation>
- <doc:FAQ-Pricing>
- <doc:FAQ-MigrateFromRevenueCat>
- <doc:FAQ-ServerLimits>


## Contact
Expand Down
6 changes: 4 additions & 2 deletions Sources/FreemiumKit/FreemiumKit.docc/SetupGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Learn how to set up your app for our paywalls and live push notifications.

@Video(poster: "PaidViews-Poster", source: "PaidViews")

1. Recommended: Lock your paid features for users who have not made a purchase yet by using one of the built-in views `PaidFeatureButton` or `PaidFeatureView`. For example:
1. Lock your paid features for users who have not made a purchase yet by using one of the built-in views `PaidFeatureButton` or `PaidFeatureView`. This is the recommended way of using the SDK (when applicable) as it handles purchase states automatically for you. For example:

```swift
// opens paywall if user has not purchased, else works like a normal (stylable) button
Expand Down Expand Up @@ -93,7 +93,7 @@ Learn how to set up your app for our paywalls and live push notifications.
}
```

If you want to conditionally hide views based on paid state (like hiding the unlock button if a user has already purchased), you can add the `FreemiumKit` object as an `@EnvironmentObject` and call `.purchasedTier` on it like so:
If you want to conditionally hide views based on paid state (like hiding the unlock button if a user has already purchased), you can add the `FreemiumKit` object as an `@EnvironmentObject` and call `.purchasedTier` (or `.hasPurchased` if you only have one tier) like so:

```swift
import FreemiumKit
Expand Down Expand Up @@ -141,6 +141,8 @@ Learn how to set up your app for our paywalls and live push notifications.
}
```

Note that you can also access the `FreemiumKit` object from your models globally by calling `FreemiumKit.shared`. But in your SwiftUI views you should use the `@EnvironmentObject` so your views get updated correctly.

1. There's also a `PaidStatusView` which you can add to your app's settings to indicate to users what their current purchase state is. There are two styles:

```swift
Expand Down

0 comments on commit 652ea74

Please sign in to comment.