-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOSIABApplicationRouterAdapter.swift
More file actions
37 lines (30 loc) · 1.68 KB
/
OSIABApplicationRouterAdapter.swift
File metadata and controls
37 lines (30 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import UIKit
/// Protocol to be implemented by the objects that can handle opening URLs.
/// This is implemented by the `UIApplication` object that can be used as an External Browser.
public protocol OSIABApplicationDelegate: AnyObject {
func canOpenURL(_ url: URL) -> Bool
func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey: Any], completionHandler completion: ((Bool) -> Void)?)
}
/// Provide a default implementations that abstracts the options parameter.
extension OSIABApplicationDelegate {
public func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey: Any] = [:], completionHandler completion: ((Bool) -> Void)?) {
self.open(url, options: options, completionHandler: completion)
}
}
/// Make `UIApplication` conform to the `OSIABApplicationDelegate` protocol.
extension UIApplication: OSIABApplicationDelegate {}
/// Adapter that makes the required calls so that an `OSIABApplicationDelegate` implementation can perform the External Browser routing.
public class OSIABApplicationRouterAdapter: OSIABRouter {
public typealias ReturnType = Bool
/// The object that will performing the URL opening.
private let application: OSIABApplicationDelegate
/// Constructor method.
/// - Parameter application: The object that will performing the URL opening.
public init(_ application: OSIABApplicationDelegate) {
self.application = application
}
public func handleOpen(_ url: URL, _ completionHandler: @escaping (ReturnType) -> Void) {
guard self.application.canOpenURL(url) else { return completionHandler(false) }
self.application.open(url, completionHandler: completionHandler)
}
}