-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
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,76 @@ | ||
// | ||
// PaywallViewController.swift | ||
// | ||
// | ||
// Created by Nacho Soto on 8/1/23. | ||
// | ||
|
||
#if canImport(UIKit) | ||
|
||
import RevenueCat | ||
import SwiftUI | ||
import UIKit | ||
|
||
/// A view controller for displaying `PaywallData` for an `Offering`. | ||
/// | ||
/// - Seealso: ``PaywallView`` for `SwiftUI`. | ||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
@objc(RCPaywallViewController) | ||
public final class PaywallViewController: UIViewController { | ||
|
||
/// See ``PaywallViewControllerDelegate`` for receiving purchase events. | ||
public weak var delegate: PaywallViewControllerDelegate? | ||
|
||
private let offering: Offering? | ||
|
||
// swiftlint:disable:next missing_docs | ||
public init(offering: Offering? = nil) { | ||
self.offering = offering | ||
|
||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
// swiftlint:disable:next missing_docs | ||
public required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private lazy var hostingController: UIHostingController<AnyView> = { | ||
let view = PaywallView(offering: self.offering) | ||
.onPurchaseCompleted { [weak self] customerInfo in | ||
guard let self = self else { return } | ||
self.delegate?.paywallViewController(self, didFinishPurchasingWith: customerInfo) | ||
} | ||
|
||
return .init(rootView: AnyView(view)) | ||
}() | ||
|
||
public override func loadView() { | ||
super.loadView() | ||
|
||
self.addChild(self.hostingController) | ||
self.view.addSubview(self.hostingController.view) | ||
self.hostingController.didMove(toParent: self) | ||
} | ||
|
||
public override func viewWillLayoutSubviews() { | ||
super.viewWillLayoutSubviews() | ||
|
||
self.hostingController.view.frame = self.view.bounds | ||
} | ||
|
||
} | ||
|
||
/// Delegate for ``PaywallViewController``. | ||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
@objc(RCPaywallViewControllerDelegate) | ||
public protocol PaywallViewControllerDelegate: AnyObject { | ||
|
||
/// Notifies that a purchase has completed in a ``PaywallViewController``. | ||
@objc(paywallViewController:didFinishPurchasingWithCustomerInfo:) | ||
func paywallViewController(_ controller: PaywallViewController, | ||
didFinishPurchasingWith customerInfo: CustomerInfo) | ||
|
||
} | ||
|
||
#endif |
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
26 changes: 26 additions & 0 deletions
26
Tests/APITesters/RevenueCatUIAPITester/SwiftAPITester/PaywallViewControllerAPI.swift
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,26 @@ | ||
// | ||
// PaywallViewControllerAPI.swift | ||
// SwiftAPITester | ||
// | ||
// Created by Nacho Soto on 8/1/23. | ||
// | ||
|
||
import RevenueCat | ||
import RevenueCatUI | ||
import SwiftUI | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
func paywallViewControllerAPI(_ delegate: Delegate, _ offering: Offering?) { | ||
let controller = PaywallViewController() | ||
controller.delegate = delegate | ||
|
||
let _: UIViewController = PaywallViewController(offering: offering) | ||
} | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
final class Delegate: PaywallViewControllerDelegate { | ||
|
||
func paywallViewController(_ controller: PaywallViewController, | ||
didFinishPurchasingWith customerInfo: CustomerInfo) {} | ||
|
||
} |