### CocoaPods Installation with Macro Plugin Source: https://github.com/reers/reerrouter/blob/main/README.md Configure your Podfile to include ReerRouter and set up the necessary build settings to load the macro plugin executable for Swift Macros. ```ruby s.pod_target_xcconfig = { 'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_ROOT}/ReerRouter/MacroPlugin/ReerRouterMacros#ReerRouterMacros' } s.user_target_xcconfig = { 'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_ROOT}/ReerRouter/MacroPlugin/ReerRouterMacros#ReerRouterMacros' } ``` -------------------------------- ### Swift Package Manager Installation Source: https://github.com/reers/reerrouter/blob/main/README.md Integrate ReerRouter into your project using Swift Package Manager by adding the Git repository URL and specifying the product dependency in your Package.swift file. ```swift // Package.swift let package = Package( name: "APackageDependOnReerRouter", platforms: [.iOS(.v13)], products: [ .library(name: "APackageDependOnReerRouter", targets: ["APackageDependOnReerRouter"]), ], dependencies: [ .package(url: "https://github.com/reers/ReerRouter.git", from: "2.3.0") ], targets: [ .target( name: "APackageDependOnReerRouter", dependencies: [ .product(name: "ReerRouter", package: "ReerRouter") ] ), ] ) ``` -------------------------------- ### Open, Push, or Present URL for ViewController Source: https://github.com/reers/reerrouter/blob/main/README.md Open, push, or present a ViewController by navigating to its URL. Supports both Mode 1 and Mode 2 routing. ```swift // Mode 1. Router.shared.open("myapp://user?name=phoenix") Router.shared.push("myapp://user?name=phoenix") Router.shared.present("myapp://user?name=phoenix") // Mode 2. Router.shared.open("myapp://phoenix.com/user?name=phoenix") Router.shared.push("myapp://phoenix.com/user?name=phoenix") Router.shared.present("myapp://phoenix.com/user?name=phoenix") ``` -------------------------------- ### CocoaPods Post-Install Script for Macro Plugin Source: https://github.com/reers/reerrouter/blob/main/README.md A post-install script for CocoaPods that adds the necessary Swift flags to load the ReerRouter macro plugin for all targets depending on ReerRouter. ```ruby post_install do |installer| installer.pods_project.targets.each do |target| reerrouter_dependency = target.dependencies.find { |d| ['ReerRouter'].include?(d.name) } if reerrouter_dependency puts "Adding Rhea Swift flags to target: #{target.name}" target.build_configurations.each do |config| swift_flags = config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)'] plugin_flag = '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_ROOT}/ReerRouter/MacroPlugin/ReerRouterMacros#ReerRouterMacros' unless swift_flags.join(' ').include?(plugin_flag) swift_flags.concat(plugin_flag.split) end end config.build_settings['OTHER_SWIFT_FLAGS'] = swift_flags end end end end ``` -------------------------------- ### Register ViewControllers by Type Names and Keys Source: https://github.com/reers/reerrouter/blob/main/README.md Register multiple ViewControllers using a dictionary where keys are route keys and values are type names as strings. ```swift Router.shared.registerPageClasses(with: ["preference": PreferenceViewController.self]) ``` -------------------------------- ### Open URL for Action or Route Source: https://github.com/reers/reerrouter/blob/main/README.md Open a URL to trigger a registered action or navigate to a route. Supports both Mode 1 and Mode 2 routing. ```swift // Mode 1. Router.shared.open("myapp://abc_action") // Mode 2. Router.shared.open("myapp://phoenix.com/abc_action") ``` -------------------------------- ### Register ViewControllers by Type Names as Strings Source: https://github.com/reers/reerrouter/blob/main/README.md Register ViewControllers using a dictionary where keys are route keys and values are the fully qualified type names as strings. ```swift Router.shared.registerPageClasses(with: ["preference": "ReerRouter_Example.PreferenceViewController"]) ``` -------------------------------- ### Configure Router Host Source: https://github.com/reers/reerrouter/blob/main/README.md Set the host for the router instance to enable Mode 2 routing. ```swift extension Router: RouterConfigable { public static var host: String { return "example.com" } } ``` -------------------------------- ### Implement Routable Protocol for ViewController Source: https://github.com/reers/reerrouter/blob/main/README.md Implement the Routable protocol for a ViewController to allow it to be instantiated by the router. Requires a `make` static function. ```swift class UserViewController: UIViewController, Routable { var params: [String: Any] init(params: [String: Any]) { self.params = params super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } static func make(with param: Route.Param) -> UserViewController? { return .init(params: param.allParams) } } ``` -------------------------------- ### Execute a Registered Action Source: https://github.com/reers/reerrouter/blob/main/README.md Execute a previously registered action by its unique key. ```swift Router.shared.executeAction(byKey: "abc_action") ``` -------------------------------- ### Register Action with @route Source: https://github.com/reers/reerrouter/blob/main/README.md Define a Route.Key and use the #route macro within a struct to register a specific action. The action is a closure that receives parameters. ```swift extension Route.Key { // Note: The variable name 'testKey' must exactly match the assigned string static let testKey: Self = "testKey" } struct Foo { #route(key: .testKey, action: { params in print("testKey triggered nested") }) } ``` -------------------------------- ### Present ViewController with Embed Option Source: https://github.com/reers/reerrouter/blob/main/README.md Present a ViewController identified by its route key, optionally embedding it within a specified navigation controller. ```swift Router.shared.present(byKey: .userPage, embedIn: UINavigationController.self, userInfo: [ "name": "apple", "id": "123123" ]) ``` -------------------------------- ### Register Action by Key Source: https://github.com/reers/reerrouter/blob/main/README.md Register a closure to be executed when a specific action key is triggered. ```swift Router.shared.registerAction(with: "abc_action") { _ in print("action executed.") } ``` -------------------------------- ### Register UIViewController with @Routable Source: https://github.com/reers/reerrouter/blob/main/README.md Define a Route.Key and annotate a UIViewController subclass with @Routable to register it. The `make` static function is used to instantiate the ViewController. ```swift extension Route.Key { // Note: The variable name 'chat' must exactly match the assigned string static let chat: Route.Key = "chat" } @Routable(.chat) class ChatViewController: UIViewController { static func make(with param: Route.Param) -> ChatViewController? { return .init() } // ... other methods ... } @Routable("setting") class SettingViewController: UIViewController { static func make(with param: Route.Param) -> SettingViewController? { return .init() } // ... other methods ... } ``` -------------------------------- ### Handle Route Will Open Notification Source: https://github.com/reers/reerrouter/blob/main/README.md Observe `Notification.Name.routeWillOpenURL` to perform actions before a route is opened. Access route parameters via `userInfo`. ```swift NotificationCenter.default.addObserver( forName: Notification.Name.routeWillOpenURL, object: nil, queue: .main ) { notification in if let param = notification.userInfo?[Route.notificationUserInfoKey] as? Route.Param { print("notification: route will open \(param.sourceURL)") } } ``` -------------------------------- ### Register ViewController by Type and Key Source: https://github.com/reers/reerrouter/blob/main/README.md Register a ViewController type with a specific route key. Static let properties can be used for keys. ```swift extension Route.Key { static let userPage: Self = "user" } Router.shared.register(UserViewController.self, forKey: .userPage) Router.shared.register(UserViewController.self, forKey: "user") ``` -------------------------------- ### Manual Route Registration Mode Source: https://github.com/reers/reerrouter/blob/main/README.md Configure the router to use manual registration by setting `registrationMode` to `.manual`. Routes must then be registered explicitly using `registerRoutes()`. ```swift extension Router: RouterConfigable { // This configuration disables automatic retrieval public static var registrationMode: RegistrationMode { return .manual } } // Then call at an appropriate time Router.shared.registerRoutes() ``` -------------------------------- ### Implement RouterDelegate for Route Events Source: https://github.com/reers/reerrouter/blob/main/README.md Implement the RouterDelegate protocol to intercept and manage routing events such as URL opening, success, failure, and fallback. ```swift extension RouteManager: RouterDelegate { func router(_ router: Router, willOpenURL url: URL, userInfo: [String : Any]) -> URL? { print("will open \(url)") if let _ = url.absoluteString.range(of: "google") { return URL(string: url.absoluteString + "&extra1=234244&extra2=afsfafasd") } else if let _ = url.absoluteString.range(of: "bytedance"), !isUserLoggedIn() { print("intercepted by delegate") return nil } return url } func router(_ router: Router, didOpenURL url: URL, userInfo: [String : Any]) { print("did open \(url) success") } func router(_ router: Router, didFailToOpenURL url: URL, userInfo: [String : Any]) { print("did fail to open \(url)") } func router(_ router: Router, didFallbackToURL url: URL, userInfo: [String: Any]) { print("did fallback to \(url)") } } ``` -------------------------------- ### Set Router Host for Mode 2 Source: https://github.com/reers/reerrouter/blob/main/README.md Configure the shared router instance's host property to enable Mode 2 routing. ```swift Router.shared.host = "phoenix.com" ``` -------------------------------- ### Implement Redirect Logic in ViewController Source: https://github.com/reers/reerrouter/blob/main/README.md Implement the `redirectURLWithRouteParam(_:)` method in a `Routable` view controller to handle URL redirects based on route parameters. ```swift class PreferenceViewController: UIViewController, Routable { static func make(with param: Route.Param) -> PreferenceViewController? { return .init() } class func redirectURLWithRouteParam(_ param: Route.Param) -> URL? { if let value = param.allParams["some_key"] as? String, value == "redirect" { return URL(string: "myapp://new_preference") } return nil } } ``` -------------------------------- ### Register Route with Swift Macro Source: https://github.com/reers/reerrouter/blob/main/README.md Utilize Swift Macros to register routes, including actions, directly within struct or class definitions. ```swift extension Route.Key { static let testKey: Self = "testKey" } struct Foo { #route(key: .testKey, action: { params in print("testKey triggered nested") }) } ``` -------------------------------- ### Handle Route Did Open Notification Source: https://github.com/reers/reerrouter/blob/main/README.md Observe `Notification.Name.routeDidOpenURL` to perform actions after a route has been opened. Access route parameters via `userInfo`. ```swift NotificationCenter.default.addObserver( forName: Notification.Name.routeDidOpenURL, object: nil, queue: .main ) { notification in if let param = notification.userInfo?[Route.notificationUserInfoKey] as? Route.Param { print("notification: route did open \(param.sourceURL)") } } ``` -------------------------------- ### Access Global Router Instance Source: https://github.com/reers/reerrouter/blob/main/README.md Access the shared router instance globally using the `AppRouter` constant for convenient route opening. ```swift public let AppRouter = Router.shared AppRouter.open("myapp://user") ``` -------------------------------- ### Declare Routable View Controllers with @Routable Macro Source: https://github.com/reers/reerrouter/blob/main/README.md Use the @Routable macro to automatically register ViewControllers for routing. Requires a static `make` function for instantiation. ```swift extension Route.Key { static let chat: Route.Key = "chat" } @Routable(.chat) class ChatViewController: UIViewController { static func make(with param: Route.Param) -> ChatViewController? { return .init() } // ... other methods ... } @Routable("setting") class SettingViewController: UIViewController { static func make(with param: Route.Param) -> SettingViewController? { return .init() } // ... other methods ... } ``` -------------------------------- ### Custom Transition Execution for Router Source: https://github.com/reers/reerrouter/blob/main/README.md Define a custom transition closure to control how view controllers are presented or pushed. The closure returns a boolean indicating if the transition was handled. ```swift public typealias UserTransition = ( _ fromNavigationController: UINavigationController?, _ fromViewController: UIViewController?, _ toViewController: UIViewController ) -> Bool public enum TransitionExecutor { /// Transition will be handled by router automatically. case router /// Transition will be handled by user who invoke the router `push` or `present` method. case user(UserTransition) /// Transition will be handled by user who invoke the router `push` or `present` method. case delegate } let transition: Route.UserTransition = { fromNavigationController, fromViewController, toViewController in toViewController.transitioningDelegate = self.animator toViewController.modalPresentationStyle = .currentContext // Use the router found view controller directly, or just handle transition by yourself. // fromViewController?.present(toViewController, animated: true) self.present(toViewController, animated: true) return true } AppRouter.present(user.urlString, transitionExecutor: .user(transition)) ``` -------------------------------- ### Set Fallback URL for Router Source: https://github.com/reers/reerrouter/blob/main/README.md Use the `route_fallback_url` key to specify a fallback URL when an error occurs during routing. ```swift Router.shared.open("myapp://unregisteredKey?route_fallback_url=myapp%3A%2F%2Fuser%3Fname%3Di_am_fallback") ``` -------------------------------- ### Disable Transition Animation with Router Source: https://github.com/reers/reerrouter/blob/main/README.md Use the `route_no_animation` query parameter to disable transition animations when opening a route. ```swift Router.shared.open("myapp://user?name=google&route_no_animation=1") ``` -------------------------------- ### Add Route Interceptor Source: https://github.com/reers/reerrouter/blob/main/README.md Add an interceptor to a specific route key to intercept and potentially block or modify route opening logic. Returning `false` intercepts the URL. ```swift Router.shared.addInterceptor(forKey: .userPage) { (_) -> Bool in print("intercepted user page") return true } Router.shared.addInterceptor(forKey: .userPage) { (params) -> Bool in print("intercepted user page") if let name = params.allParams["name"] as? String, name == "google" { print("intercepted user page success") return false } return true } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.