Skip to content

Commit

Permalink
Merge pull request #2622 from AlphaWallet/support-awallet-custom-url-…
Browse files Browse the repository at this point in the history
…scheme-for-just-switching-to-app-no-op

Support awallet: custom URL scheme for just switching to app and doing nothing else
  • Loading branch information
vladyslav-iosdev authored Mar 9, 2021
2 parents 8865173 + 0c19024 commit 95acc96
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions AlphaWallet/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ extension AppCoordinator: InCoordinatorDelegate {
guard universalLinkCoordinator == nil else { return }
handleUniversalLink(url: url)
}

func handleCustomUrlScheme(_ url: URL, forCoordinator coordinator: InCoordinator) {
_ = handleOpen(url: url)
}
}

extension AppCoordinator: UniversalLinkCoordinatorDelegate {
Expand Down
5 changes: 5 additions & 0 deletions AlphaWallet/Browser/Coordinators/DappBrowserCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protocol DappBrowserCoordinatorDelegate: class {
func didSentTransaction(transaction: SentTransaction, inCoordinator coordinator: DappBrowserCoordinator)
func importUniversalLink(url: URL, forCoordinator coordinator: DappBrowserCoordinator)
func handleUniversalLink(_ url: URL, forCoordinator coordinator: DappBrowserCoordinator)
func handleCustomUrlScheme(_ url: URL, forCoordinator coordinator: DappBrowserCoordinator)
}

final class DappBrowserCoordinator: NSObject, Coordinator {
Expand Down Expand Up @@ -470,6 +471,10 @@ extension DappBrowserCoordinator: BrowserViewControllerDelegate {
func handleUniversalLink(_ url: URL, inBrowserViewController viewController: BrowserViewController) {
delegate?.handleUniversalLink(url, forCoordinator: self)
}

func handleCustomUrlScheme(_ url: URL, inBrowserViewController viewController: BrowserViewController) {
delegate?.handleCustomUrlScheme(url, forCoordinator: self)
}
}

extension DappBrowserCoordinator: BrowserHistoryViewControllerDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol BrowserViewControllerDelegate: class {
func dismissKeyboard(inBrowserViewController viewController: BrowserViewController)
func forceUpdate(url: URL, inBrowserViewController viewController: BrowserViewController)
func handleUniversalLink(_ url: URL, inBrowserViewController viewController: BrowserViewController)
func handleCustomUrlScheme(_ url: URL, inBrowserViewController viewController: BrowserViewController)
}

final class BrowserViewController: UIViewController {
Expand Down Expand Up @@ -226,6 +227,10 @@ extension BrowserViewController: WKNavigationDelegate {
delegate?.handleUniversalLink(url, inBrowserViewController: self)
return decisionHandler(.cancel)
}
if url.scheme == ShareContentAction.scheme {
delegate?.handleCustomUrlScheme(url, inBrowserViewController: self)
return decisionHandler(.cancel)
}

decisionHandler(.allow)
}
Expand Down
5 changes: 5 additions & 0 deletions AlphaWallet/InCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol InCoordinatorDelegate: class {
func assetDefinitionsOverrideViewController(for coordinator: InCoordinator) -> UIViewController?
func importUniversalLink(url: URL, forCoordinator coordinator: InCoordinator)
func handleUniversalLink(_ url: URL, forCoordinator coordinator: InCoordinator)
func handleCustomUrlScheme(_ url: URL, forCoordinator coordinator: InCoordinator)
}

enum Tabs {
Expand Down Expand Up @@ -912,6 +913,10 @@ extension InCoordinator: DappBrowserCoordinatorDelegate {
func handleUniversalLink(_ url: URL, forCoordinator coordinator: DappBrowserCoordinator) {
delegate?.handleUniversalLink(url, forCoordinator: self)
}

func handleCustomUrlScheme(_ url: URL, forCoordinator coordinator: DappBrowserCoordinator) {
delegate?.handleCustomUrlScheme(url, forCoordinator: self)
}
}

extension InCoordinator: StaticHTMLViewControllerDelegate {
Expand Down
16 changes: 14 additions & 2 deletions AlphaWallet/Share/ShareContentAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
import Foundation

enum ShareContentAction {

static let scheme = "awallet"

private enum Host: String {
case openURL
case openText
case openApp
}

case url(URL)
case string(String)
case openApp

init?(_ url: URL) {
guard let scheme = url.scheme, scheme == ShareContentAction.scheme, let hostValue = url.host, let host = Host(rawValue: hostValue) else {
guard let scheme = url.scheme, scheme == ShareContentAction.scheme else {
return nil
}
guard let hostValue = url.host, let host = Host(rawValue: hostValue) else {
self = .openApp
return
}

let components = URLComponents(url: url, resolvingAgainstBaseURL: true)
switch host {
Expand All @@ -36,6 +41,9 @@ enum ShareContentAction {
return nil
}
self = .string(value)
case .openApp:
//Impossible to reach here
return nil
}
}

Expand All @@ -45,6 +53,8 @@ enum ShareContentAction {
return Host.openURL.rawValue
case .string:
return Host.openText.rawValue
case .openApp:
return ""
}
}

Expand All @@ -67,6 +77,8 @@ enum ShareContentAction {
components.queryItems = [
.init(name: "q", value: text)
]
case .openApp:
return .init()
}

return components
Expand Down
5 changes: 4 additions & 1 deletion AlphaWallet/Share/UrlSchemeCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ class UrlSchemeCoordinator: UrlSchemeCoordinatorType {
break //NOTE: here we can add parsing Addresses from string
case .url(let url):
inCoordinator.openURLInBrowser(url: url)
case .openApp:
//No-op. Just switching to the app
break
}

pendingUrl = .none
pendingUrl = .none
}

private func canHandle(url: URL) -> Bool {
Expand Down

0 comments on commit 95acc96

Please sign in to comment.