feat(mobile): add RNS base view controller and navigation utilities

- Create RNSViewController as a base view controller
- Update WebViewController to inherit from RNSViewController
- Add UIWindow extension to find RNS navigation controller
- Modify HelperModule to use new navigation and web view utilities
- Refactor WebViewManager to use WebViewController for modal views

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-02-27 21:09:14 +08:00
parent 6c2bc6633e
commit d45ba9ecfe
5 changed files with 96 additions and 14 deletions
@@ -0,0 +1,13 @@
//
// RNSViewController.swift
// FollowNative
//
// Created by Innei on 2025/2/27.
//
import UIKit
class RNSViewController: UIViewController {
@objc public let screenView: UIView? = nil
}
@@ -10,7 +10,7 @@ import SnapKit
import UIKit
import WebKit
class WebViewController: UIViewController {
class WebViewController: RNSViewController {
private let webView: WKWebView
private let url: URL
@@ -53,10 +53,9 @@ class WebViewController: UIViewController {
}
private func setupToolbar() {
// 确保导航控制器显示工具栏
navigationController?.isToolbarHidden = false
// 创建工具栏按钮
let backButton = UIBarButtonItem(
image: UIImage(systemName: "chevron.backward"), style: .plain, target: self,
action: #selector(goBack))
@@ -76,10 +75,8 @@ class WebViewController: UIViewController {
action: #selector(openInSafari))
safariButton.tintColor = Utils.accentColor
// 添加弹性空间使按钮均匀分布
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
// 设置工具栏项目
toolbarItems = [
flexSpace, backButton, flexSpace, forwardButton, flexSpace, refreshButton, flexSpace,
safariButton, flexSpace,
@@ -117,24 +114,22 @@ extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
navigationItem.title = webView.title
// 更新后退/前进按钮状态
updateToolbarButtonsState()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// 更新后退/前进按钮状态
updateToolbarButtonsState()
}
private func updateToolbarButtonsState() {
// 找到后退和前进按钮并更新它们的启用状态
if let items = toolbarItems {
// 后退按钮在索引1
if let backButton = items[1] as? UIBarButtonItem {
backButton.isEnabled = webView.canGoBack
}
// 前进按钮在索引3
if let forwardButton = items[3] as? UIBarButtonItem {
forwardButton.isEnabled = webView.canGoForward
}
@@ -0,0 +1,62 @@
//
// UIWindow.swift
// FollowNative
//
// Created by Innei on 2025/2/27.
//
import UIKit
extension UIWindow {
public static func findRNSNavigationController() -> UINavigationController? {
guard let window = UIApplication.shared.windows.first else {
return nil
}
let rootViewController = window.rootViewController
if let navController = rootViewController as? UINavigationController,
NSStringFromClass(type(of: navController)).contains("RNSNavigationController")
{
return navController
}
return findRNSNavigationControllerInChildren(of: rootViewController)
}
private static func findRNSNavigationControllerInChildren(of viewController: UIViewController?)
-> UINavigationController?
{
guard let viewController = viewController else {
return nil
}
if let presentedVC = viewController.presentedViewController {
if let navController = presentedVC as? UINavigationController,
NSStringFromClass(type(of: navController)).contains("RNSNavigationController")
{
return navController
}
if let result = findRNSNavigationControllerInChildren(of: presentedVC) {
return result
}
}
for childVC in viewController.children {
if let navController = childVC as? UINavigationController,
NSStringFromClass(type(of: navController)).contains("RNSNavigationController")
{
return navController
}
if let result = findRNSNavigationControllerInChildren(of: childVC) {
return result
}
}
return nil
}
}
@@ -7,7 +7,10 @@
import ExpoModulesCore
import UIKit
public class HelperModule: Module {
public func definition() -> ExpoModulesCore.ModuleDefinition {
Name("Helper")
@@ -16,8 +19,17 @@ public class HelperModule: Module {
return
}
DispatchQueue.main.async {
// guard let rootVC = UIApplication.shared.windows.first?.rootViewController else { return }
// WebViewManager.pushModalWebView(url: url, from: rootVC)
//
// if let navVC = UIWindow.findRNSNavigationController() as? UINavigationController {
// debugPrint(navVC)
//
// navVC.pushViewController(WebViewController(url: url), animated: true)
//// WebViewManager.pushModalWebView(url: url, from: navVC)
//
// }
guard let rootVC = UIApplication.shared.windows.first?.rootViewController else { return }
WebViewManager.presentModalWebView(url: url, from: rootVC)
}
}
@@ -80,7 +92,7 @@ public class HelperModule: Module {
extension UIScrollView {
func scrollToTopIfPossible(animated: Bool) {
let encodedSelector = "X3Njcm9sbFRvVG9wSWZQb3NzaWJsZTo=" // "_scrollToTopIfPossible:" 的 Base64 编码
let encodedSelector = "X3Njcm9sbFRvVG9wSWZQb3NzaWJsZTo=" // "_scrollToTopIfPossible:"
if let decodedData = Data(base64Encoded: encodedSelector),
let decodedString = String(data: decodedData, encoding: .utf8)
@@ -177,7 +177,7 @@ enum WebViewManager {
}
static func pushModalWebView(url: URL, from navigationController: UINavigationController) {
let modalVC = ModalWebViewController(url: url)
let modalVC = WebViewController(url: url)
navigationController.pushViewController(modalVC, animated: true)
}
}