Skip to content

Instantly share code, notes, and snippets.

@HelloNamiML
HelloNamiML / view_visibility_example.swift
Last active March 22, 2021 19:39
View Visibility Based on Subscription State
var body: some View {
VStack(spacing: 30) {
if subscriptionDataSource.subscribed {
SubscribedView()
} else {
DefaultView()
}
}
}
@HelloNamiML
HelloNamiML / access_observableobject_in_view.swift
Last active March 22, 2021 19:40
Access the Subscription Object in a View
struct ExampleView: View {
@EnvironmentObject var subscriptionDataSource: SubscriptionDataSource
var body: some View {
if subscriptionDataSource.subscribed {
// react to active subscription
}
}
}
@HelloNamiML
HelloNamiML / setup_observable_object.swift
Last active March 22, 2021 19:40
Setup your Observable Object to be Accessible in Your App
@main
struct ExampleApp: App {
init() {
// Start up or configure your IAP transaction handling code here
}
var subscriptionDataSource = SubscriptionDataSource()
var body: some Scene {
WindowGroup {
@HelloNamiML
HelloNamiML / subscription_observable_object_nami.swift
Created March 22, 2021 17:27
Observable Object for Subscriptions using Nami
class NamiSubscriptionDataSource: ObservableObject {
@Published var subscribed = false
@Published var expirationDate: Date?
init() {
// On first init, check to see if subscription is active.
subscribed = (NamiEntitlementManager.activeEntitlements().count > 0)
// when there is a change to entitlements, updated subscribed state
NamiEntitlementManager.registerChangeHandler { (activeEntitlements) in
@HelloNamiML
HelloNamiML / storekit_on_purchase.swift
Last active March 24, 2021 19:31
On Successful Purchase, Update Observable Object
import StoreKit
class StoreObserver: NSObject, SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
nc.post(name: NSNotification.Name(rawValue: "PurchaseNotification"), object: nil, userInfo: ["subscribed": true])
break
@unknown default:
@HelloNamiML
HelloNamiML / subscription_observable_object.swift
Created March 21, 2021 01:57
Add init Method to Observable Object for Subscriptions
class SubscriptionDataSource: ObservableObject {
@Published var subscribed = false
@Published var expirationDate: Date?
private var listener: NSObjectProtocol?
init() {
// On first init, check to see if subscription is active.
subscribed = UserDefaults.standard.bool(forKey: "AppIsSubscribed")
listener = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "PurchaseNotification"), object: nil, queue: .main) { (notification) in
@HelloNamiML
HelloNamiML / basic_subscription_observable_object.swift
Last active March 21, 2021 01:31
Basic Subscription Observable Object for SwiftUI
class SubscriptionDataSource: ObservableObject {
@Published var subscribed = false
@Published var expirationDate: Date?
}
@HelloNamiML
HelloNamiML / consumable_state.swift
Created March 20, 2021 18:03
Consumable State for SwiftUI IAPs
creditBalance: Int
purchased: Bool
purchaseCount: Int
@HelloNamiML
HelloNamiML / one_time_purchase_state.swift
Created March 20, 2021 17:50
One-Time Purchase State for SwiftUI IAPs
purchased: Bool
@HelloNamiML
HelloNamiML / subscription_state.swift
Created March 20, 2021 17:46
Subscription State for SwiftUI IAPs
subscribed : Bool
expiration: Date
autoRenewing: Bool