diff --git a/apps/mobile/native/ios/Controllers/RNSViewController.swift b/apps/mobile/native/ios/Controllers/RNSViewController.swift new file mode 100644 index 000000000..b522fed40 --- /dev/null +++ b/apps/mobile/native/ios/Controllers/RNSViewController.swift @@ -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 + +} diff --git a/apps/mobile/native/ios/Controllers/WebViewController.swift b/apps/mobile/native/ios/Controllers/WebViewController.swift index 363f87df8..1c4c6fa89 100644 --- a/apps/mobile/native/ios/Controllers/WebViewController.swift +++ b/apps/mobile/native/ios/Controllers/WebViewController.swift @@ -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 } diff --git a/apps/mobile/native/ios/Extensions/UIWindow.swift b/apps/mobile/native/ios/Extensions/UIWindow.swift new file mode 100644 index 000000000..a1fabb430 --- /dev/null +++ b/apps/mobile/native/ios/Extensions/UIWindow.swift @@ -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 + } +} diff --git a/apps/mobile/native/ios/Helper/HelperModule.swift b/apps/mobile/native/ios/Helper/HelperModule.swift index 5c00775cc..8ca778367 100644 --- a/apps/mobile/native/ios/Helper/HelperModule.swift +++ b/apps/mobile/native/ios/Helper/HelperModule.swift @@ -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) diff --git a/apps/mobile/native/ios/SharedWebView/WebViewManager.swift b/apps/mobile/native/ios/SharedWebView/WebViewManager.swift index 8b37d0f9e..a1c5fd6db 100644 --- a/apps/mobile/native/ios/SharedWebView/WebViewManager.swift +++ b/apps/mobile/native/ios/SharedWebView/WebViewManager.swift @@ -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) } }