-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathReactNativeViewController.swift
66 lines (53 loc) · 2.06 KB
/
ReactNativeViewController.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import UIKit
import React
@objc public class ReactNativeViewController: UIViewController {
private var moduleName: String
private var initialProperties: [String: Any]?
@objc public init(moduleName: String, initialProperties: [String: Any]? = nil) {
self.moduleName = moduleName
self.initialProperties = initialProperties
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func viewDidLoad() {
super.viewDidLoad()
guard let factory = ReactNativeBrownfield.shared.rootViewFactory else {
print("Error: You need to start React Native in order to use ReactNativeViewController, make sure to run BridgeManager.shared.startReactNative() before instantiating it.")
return
}
if !moduleName.isEmpty {
view = factory.view(withModuleName: moduleName, initialProperties: initialProperties)
NotificationCenter.default.addObserver(
self,
selector: #selector(togglePopGestureRecognizer(_:)),
name: NSNotification.Name.togglePopGestureRecognizer,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(popToNative(_:)),
name: NSNotification.Name.popToNative,
object: nil
)
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc private func togglePopGestureRecognizer(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let enabled = userInfo["enabled"] as? Bool else { return }
DispatchQueue.main.async { [weak self] in
self?.navigationController?.interactivePopGestureRecognizer?.isEnabled = enabled
}
}
@objc private func popToNative(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let animated = userInfo["animated"] as? Bool else { return }
DispatchQueue.main.async { [weak self] in
self?.navigationController?.popViewController(animated: animated)
}
}
}