-
Notifications
You must be signed in to change notification settings - Fork 295
/
BTAppContextSwitcher.swift
46 lines (40 loc) · 2.47 KB
/
BTAppContextSwitcher.swift
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
38
39
40
41
42
43
44
45
46
import UIKit
/// Handles return URLs when returning from app context switch and routes the return URL to the correct app context switch client class.
/// - Note: `returnURLScheme` must contain your app's registered URL Type that starts with the app's bundle ID.
/// When your app returns from app switch, the app delegate should call `handleOpenURL` (or `handleOpen` if not using SceneDelegate)
@objcMembers public class BTAppContextSwitcher: NSObject {
/// Singleton for shared instance of `BTAppContextSwitcher`
public static let sharedInstance = BTAppContextSwitcher()
/// The URL scheme to return to this app after switching to another app or opening a SFSafariViewController.
/// This URL scheme must be registered as a URL Type in the app's info.plist, and it must start with the app's bundle ID.
public var returnURLScheme: String = ""
private var appContextSwitchClients = [BTAppContextSwitchClient.Type]()
/// :nodoc: Determine whether the return URL can be handled.
/// - Parameters: url the URL you receive in `scene:openURLContexts:` (or `application:openURL:options:` if not using SceneDelegate) when returning to your app
/// - Returns: `true` when the SDK can process the return URL
@_documentation(visibility: private)
@objc(handleOpenURLContext:)
public func handleOpenURL(context: UIOpenURLContext) -> Bool {
handleOpen(context.url)
}
/// :nodoc: Complete payment flow after returning from app or browser switch.
/// - Parameter url: The URL you receive in `scene:openURLContexts:` (or `application:openURL:options:` if not using SceneDelegate)
/// - Returns: `true` when the SDK has handled the URL successfully
@_documentation(visibility: private)
@objc(handleOpenURL:)
public func handleOpen(_ url: URL) -> Bool {
for appContextSwitchClient in appContextSwitchClients {
if appContextSwitchClient.canHandleReturnURL(url) {
appContextSwitchClient.handleReturnURL(url)
return true
}
}
return false
}
/// Registers a class `Type` that can handle a return from app context switch with a static method.
/// - Parameter client: A class `Type` that implements `BTAppContextSwitchClient`, the methods of which will be invoked statically on the class.
@objc(registerAppContextSwitchClient:)
public func register(_ client: BTAppContextSwitchClient.Type) {
appContextSwitchClients.append(client)
}
}