diff --git a/Sources/ShopifyCheckout/CheckoutViewController.swift b/Sources/ShopifyCheckout/CheckoutViewController.swift index ffb17fe5..f842013d 100644 --- a/Sources/ShopifyCheckout/CheckoutViewController.swift +++ b/Sources/ShopifyCheckout/CheckoutViewController.swift @@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO import UIKit import WebKit -class CheckoutViewController: UIViewController { +class CheckoutViewController: UIViewController, UIAdaptivePresentationControllerDelegate { // MARK: Properties @@ -106,6 +106,14 @@ class CheckoutViewController: UIViewController { } @IBAction internal func close() { + didCancel() + } + + internal func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { + didCancel() + } + + private func didCancel() { CheckoutView.invalidate() delegate?.checkoutDidCancel() } diff --git a/Sources/ShopifyCheckout/ShopifyCheckout.swift b/Sources/ShopifyCheckout/ShopifyCheckout.swift index 5c139ff9..47a36480 100644 --- a/Sources/ShopifyCheckout/ShopifyCheckout.swift +++ b/Sources/ShopifyCheckout/ShopifyCheckout.swift @@ -46,8 +46,8 @@ public func preload(checkout url: URL) { /// Presents the checkout from a given `UIViewController`. public func present(checkout url: URL, from: UIViewController, delegate: CheckoutDelegate? = nil) { - let viewController = UINavigationController( - rootViewController: CheckoutViewController(checkoutURL: url, delegate: delegate) - ) + let rootViewController = CheckoutViewController(checkoutURL: url, delegate: delegate) + let viewController = UINavigationController(rootViewController: rootViewController) + viewController.presentationController?.delegate = rootViewController from.present(viewController, animated: true) } diff --git a/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift b/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift index 72f4713d..43605e90 100644 --- a/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift +++ b/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift @@ -75,6 +75,18 @@ class CheckoutViewDelegateTests: XCTestCase { XCTAssertNotEqual(two, three) } + func testPresentationControllerDidDismissInvalidatesViewCache() { + let one = CheckoutView.for(checkout: checkoutURL) + let two = CheckoutView.for(checkout: checkoutURL) + XCTAssertEqual(one, two) + + let presentationController = UIViewController().presentationController! + viewController.presentationControllerDidDismiss(presentationController) + + let three = CheckoutView.for(checkout: checkoutURL) + XCTAssertNotEqual(two, three) + } + func testCheckoutViewDidClickLinkDoesNotInvalidateViewCache() { let one = CheckoutView.for(checkout: checkoutURL) let two = CheckoutView.for(checkout: checkoutURL)