This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var body: some View { | |
VStack(spacing: 30) { | |
if subscriptionDataSource.subscribed { | |
SubscribedView() | |
} else { | |
DefaultView() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ExampleView: View { | |
@EnvironmentObject var subscriptionDataSource: SubscriptionDataSource | |
var body: some View { | |
if subscriptionDataSource.subscribed { | |
// react to active subscription | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@main | |
struct ExampleApp: App { | |
init() { | |
// Start up or configure your IAP transaction handling code here | |
} | |
var subscriptionDataSource = SubscriptionDataSource() | |
var body: some Scene { | |
WindowGroup { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubscriptionDataSource: ObservableObject { | |
@Published var subscribed = false | |
@Published var expirationDate: Date? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
creditBalance: Int | |
purchased: Bool | |
purchaseCount: Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
purchased: Bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
subscribed : Bool | |
expiration: Date | |
autoRenewing: Bool |