diff --git a/Sources/ShopifyCheckoutSheetKit/CheckoutViewController.swift b/Sources/ShopifyCheckoutSheetKit/CheckoutViewController.swift index fbc730c7..9ff4e84b 100644 --- a/Sources/ShopifyCheckoutSheetKit/CheckoutViewController.swift +++ b/Sources/ShopifyCheckoutSheetKit/CheckoutViewController.swift @@ -25,7 +25,7 @@ import UIKit import SwiftUI public class CheckoutViewController: UINavigationController { - public init(checkout url: URL, delegate: CheckoutDelegate) { + public init(checkout url: URL, delegate: CheckoutDelegate? = nil) { let rootViewController = CheckoutWebViewController(checkoutURL: url, delegate: delegate) rootViewController.notifyPresented() super.init(rootViewController: rootViewController) @@ -52,7 +52,7 @@ extension CheckoutViewController { } public func makeUIViewController(context: Self.Context) -> CheckoutViewController { - return CheckoutViewController(checkout: checkoutURL!, delegate: delegate!) + return CheckoutViewController(checkout: checkoutURL!, delegate: delegate) } public func updateUIViewController(_ uiViewController: CheckoutViewController, context: Self.Context) { diff --git a/Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift b/Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift index ef41283a..3d1bed14 100644 --- a/Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift +++ b/Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift @@ -63,7 +63,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl // MARK: Properties - var delegate: CheckoutDelegate + var delegate: CheckoutDelegate? internal var checkoutView: CheckoutWebView @@ -87,7 +87,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl // MARK: Initializers - public init(checkoutURL url: URL, delegate: CheckoutDelegate) { + public init(checkoutURL url: URL, delegate: CheckoutDelegate? = nil) { self.checkoutURL = url self.delegate = delegate @@ -189,7 +189,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl CheckoutWebView.invalidate() } - delegate.checkoutDidCancel() + delegate?.checkoutDidCancel() } } @@ -208,23 +208,23 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate { func checkoutViewDidCompleteCheckout(event: CheckoutCompletedEvent) { ConfettiCannon.fire(in: view) CheckoutWebView.invalidate() - delegate.checkoutDidComplete(event: event) + delegate?.checkoutDidComplete(event: event) } func checkoutViewDidFailWithError(error: CheckoutError) { CheckoutWebView.invalidate() - let shouldAttemptRecovery = delegate.shouldRecoverFromError(error: error) + let shouldAttemptRecovery = delegate?.shouldRecoverFromError(error: error) ?? false if shouldAttemptRecovery { self.presentFallbackViewController(url: self.checkoutURL) } else { - delegate.checkoutDidFail(error: error) + delegate?.checkoutDidFail(error: error) } } func checkoutViewDidClickLink(url: URL) { - delegate.checkoutDidClickLink(url: url) + delegate?.checkoutDidClickLink(url: url) } func checkoutViewDidToggleModal(modalVisible: Bool) { @@ -236,7 +236,7 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate { } func checkoutViewDidEmitWebPixelEvent(event: PixelEvent) { - delegate.checkoutDidEmitWebPixelEvent(event: event) + delegate?.checkoutDidEmitWebPixelEvent(event: event) } private func isErrorRecoverable(error: CheckoutError) -> Bool { diff --git a/Sources/ShopifyCheckoutSheetKit/ShopifyCheckoutSheetKit.swift b/Sources/ShopifyCheckoutSheetKit/ShopifyCheckoutSheetKit.swift index 4d84b872..a77afc5e 100644 --- a/Sources/ShopifyCheckoutSheetKit/ShopifyCheckoutSheetKit.swift +++ b/Sources/ShopifyCheckoutSheetKit/ShopifyCheckoutSheetKit.swift @@ -55,7 +55,7 @@ public func preload(checkout url: URL) { /// Presents the checkout from a given `UIViewController`. @discardableResult -public func present(checkout url: URL, from: UIViewController, delegate: CheckoutDelegate) -> CheckoutViewController { +public func present(checkout url: URL, from: UIViewController, delegate: CheckoutDelegate?) -> CheckoutViewController { let viewController = CheckoutViewController(checkout: url, delegate: delegate) from.present(viewController, animated: true) return viewController