From 2569581b5acb2982af255f06ffa2f45a8ac79aef Mon Sep 17 00:00:00 2001 From: Cian Buckley Date: Mon, 9 Oct 2023 16:48:15 +0200 Subject: [PATCH] update external link interceptor with optional param to override default behaviour --- Sources/ShopifyCheckout/CheckoutView.swift | 9 ++++++++- .../CheckoutViewTests.swift | 17 +++++++++++++++++ .../Mocks/MockNavigationAction.swift | 7 +++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Sources/ShopifyCheckout/CheckoutView.swift b/Sources/ShopifyCheckout/CheckoutView.swift index e17a153f..d4be1eb0 100644 --- a/Sources/ShopifyCheckout/CheckoutView.swift +++ b/Sources/ShopifyCheckout/CheckoutView.swift @@ -170,7 +170,14 @@ extension CheckoutView: WKNavigationDelegate { } private func isExternalLink(_ action: WKNavigationAction) -> Bool { - return action.navigationType == .linkActivated && action.targetFrame == nil + if action.navigationType == .linkActivated && action.targetFrame == nil { return true } + + guard let url = action.request.url else { return false } + guard let url = URLComponents(string: url.absoluteString) else { return false } + + guard let openExternally = url.queryItems?.first(where: { $0.name == "open_externally"})?.value else { return false } + + return openExternally == "true" } private func isMailOrTelLink(_ url: URL) -> Bool { diff --git a/Tests/ShopifyCheckoutTests/CheckoutViewTests.swift b/Tests/ShopifyCheckoutTests/CheckoutViewTests.swift index 5e60ec80..f22fd562 100644 --- a/Tests/ShopifyCheckoutTests/CheckoutViewTests.swift +++ b/Tests/ShopifyCheckoutTests/CheckoutViewTests.swift @@ -86,6 +86,23 @@ class CheckoutViewTests: XCTestCase { wait(for: [didClickLinkExpectation], timeout: 1) } + func testURLLinkDelegationWithExternalParam() { + let link = URL(string: "https://www.shopify.com/legal/privacy/app-users?open_externally=true")! + + let delegate = MockCheckoutViewDelegate() + let didClickLinkExpectation = expectation( + description: "checkoutViewDidClickLink was called" + ) + delegate.didClickLinkExpectation = didClickLinkExpectation + view.viewDelegate = delegate + + view.webView(view, decidePolicyFor: MockExternalNavigationAction(url: link, navigationType: .other)) { policy in + XCTAssertEqual(policy, .cancel) + } + + wait(for: [didClickLinkExpectation], timeout: 1) + } + func test410responseOnCheckoutURLCodeDelegation() { view.load(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!) let link = view.url! diff --git a/Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift b/Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift index ea0ad805..aed4dd56 100644 --- a/Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift +++ b/Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift @@ -38,21 +38,24 @@ class MockNavigationAction: WKNavigationAction { class MockExternalNavigationAction: WKNavigationAction { private let mockRequest: URLRequest + private let navType: WKNavigationType override var request: URLRequest { return mockRequest } override var navigationType: WKNavigationType { - return .linkActivated + return self.navType } override var targetFrame: WKFrameInfo? { return nil } - init(url: URL) { + init(url: URL, navigationType: WKNavigationType = .linkActivated) { self.mockRequest = URLRequest(url: url) + self.navType = navigationType super.init() } } +