-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paywalls
: added support for purchasing (#2812)
This adds a new `PurchaseHandler` type, which can be instantiated with `Purchases` or with a mock implementation for previews. That gets injected, and templates can now purchase and dismiss themselves. I've improved the `SimpleApp` setup to better work with the lifetime of purchases. Now it only displays the paywall if the user doesn't have an entitlement. I've also added some basic error handling to the new `AsyncButton`: ![Simulator Screenshot - iPhone 14 Pro - 2023-07-13 at 15 32 06](https://github.com/RevenueCat/purchases-ios/assets/685609/e8c2e6e3-f1e0-411f-9ca2-8a3cc3823a42)
- Loading branch information
Showing
13 changed files
with
428 additions
and
47 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// PurchaseHandler.swift | ||
// | ||
// | ||
// Created by Nacho Soto on 7/13/23. | ||
// | ||
|
||
import RevenueCat | ||
import StoreKit | ||
|
||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) | ||
final class PurchaseHandler: ObservableObject { | ||
|
||
typealias PurchaseBlock = @Sendable (Package) async throws -> PurchaseResultData | ||
|
||
let purchaseBlock: PurchaseBlock | ||
|
||
convenience init(purchases: Purchases = .shared) { | ||
self.init { package in | ||
return try await purchases.purchase(package: package) | ||
} | ||
} | ||
|
||
init(purchase: @escaping PurchaseBlock) { | ||
self.purchaseBlock = purchase | ||
} | ||
|
||
} | ||
|
||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) | ||
extension PurchaseHandler { | ||
|
||
func purchase(package: Package) async throws -> PurchaseResultData { | ||
return try await self.purchaseBlock(package) | ||
} | ||
|
||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// AsyncButton.swift | ||
// | ||
// | ||
// Created by Nacho Soto on 7/13/23. | ||
// | ||
|
||
import RevenueCat | ||
import SwiftUI | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
struct AsyncButton<Label>: View where Label: View { | ||
|
||
typealias Action = @Sendable () async throws -> Void | ||
|
||
private let action: Action | ||
private let label: Label | ||
|
||
@State | ||
private var error: LocalizedAlertError? | ||
|
||
@State | ||
private var inProgress: Bool = false | ||
|
||
init(action: @escaping Action, @ViewBuilder label: () -> Label) { | ||
self.action = action | ||
self.label = label() | ||
} | ||
|
||
var body: some View { | ||
Button { | ||
Task<Void, Never> { | ||
self.inProgress = true | ||
defer { self.inProgress = false } | ||
|
||
do { | ||
try await self.action() | ||
} catch let error as NSError { | ||
self.error = .init(error: error) | ||
} | ||
} | ||
} label: { | ||
self.label | ||
} | ||
.disabled(self.inProgress) | ||
.alert(isPresented: self.isShowingError, error: self.error) { _ in | ||
Button { | ||
self.error = nil | ||
} label: { | ||
Text("OK") | ||
} | ||
} message: { error in | ||
Text(error.failureReason ?? "") | ||
} | ||
} | ||
|
||
private var isShowingError: Binding<Bool> { | ||
return .init { | ||
self.error != nil | ||
} set: { newValue in | ||
if !newValue { | ||
self.error = nil | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// MARK: - Errors | ||
|
||
private struct LocalizedAlertError: LocalizedError { | ||
|
||
private let underlyingError: NSError | ||
|
||
init(error: NSError) { | ||
self.underlyingError = error | ||
} | ||
|
||
var errorDescription: String? { | ||
return "\(self.underlyingError.domain) \(self.underlyingError.code)" | ||
} | ||
|
||
var failureReason: String? { | ||
if let errorCode = self.underlyingError as? ErrorCode { | ||
return errorCode.description | ||
} else { | ||
return self.underlyingError.localizedDescription | ||
} | ||
} | ||
|
||
var recoverySuggestion: String? { | ||
self.underlyingError.localizedRecoverySuggestion | ||
} | ||
|
||
} |
File renamed without changes.
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
Oops, something went wrong.