|
| 1 | +# Native Integration |
| 2 | + |
| 3 | +After codegen, implement the generated delegate interface in your host app and register it before JavaScript uses the module. |
| 4 | + |
| 5 | +## Android |
| 6 | + |
| 7 | +### 1) Implement `BrownfieldNavigationDelegate` |
| 8 | + |
| 9 | +Implement the generated delegate methods in your host `Activity` (or another class with access to navigation context): |
| 10 | + |
| 11 | +```kotlin |
| 12 | +import android.content.Intent |
| 13 | +import androidx.appcompat.app.AppCompatActivity |
| 14 | +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationDelegate |
| 15 | + |
| 16 | +class MainActivity : AppCompatActivity(), BrownfieldNavigationDelegate { |
| 17 | + override fun navigateToSettings() { |
| 18 | + startActivity(Intent(this, SettingsActivity::class.java)) |
| 19 | + } |
| 20 | + |
| 21 | + override fun navigateToReferrals(userId: String) { |
| 22 | + startActivity( |
| 23 | + Intent(this, ReferralsActivity::class.java) |
| 24 | + .putExtra(ReferralsActivity.EXTRA_USER_ID, userId) |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### 2) Register the delegate during startup |
| 31 | + |
| 32 | +Register before any React Native screen can call `BrownfieldNavigation.*`: |
| 33 | + |
| 34 | +```kotlin |
| 35 | +import android.os.Bundle |
| 36 | +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationManager |
| 37 | + |
| 38 | +override fun onCreate(savedInstanceState: Bundle?) { |
| 39 | + super.onCreate(savedInstanceState) |
| 40 | + BrownfieldNavigationManager.setDelegate(this) |
| 41 | + // Initialize React Native host |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## iOS |
| 46 | + |
| 47 | +### 1) Implement `BrownfieldNavigationDelegate` |
| 48 | + |
| 49 | +```swift |
| 50 | +import BrownfieldNavigation |
| 51 | +import SwiftUI |
| 52 | +import UIKit |
| 53 | + |
| 54 | +public final class RNNavigationDelegate: BrownfieldNavigationDelegate { |
| 55 | + public func navigateToSettings() { |
| 56 | + present(SettingsScreen()) |
| 57 | + } |
| 58 | + |
| 59 | + public func navigateToReferrals(userId: String) { |
| 60 | + present(ReferralsScreen(userId: userId)) |
| 61 | + } |
| 62 | + |
| 63 | + private func present<Content: View>(_ view: Content) { |
| 64 | + DispatchQueue.main.async { |
| 65 | + let hostingController = UIHostingController(rootView: view) |
| 66 | + UIApplication.shared.topMostViewController()? |
| 67 | + .present(hostingController, animated: true) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### 2) Register the delegate at app startup |
| 74 | + |
| 75 | +```swift |
| 76 | +import BrownfieldNavigation |
| 77 | + |
| 78 | +@main |
| 79 | +struct BrownfieldAppleApp: App { |
| 80 | + init() { |
| 81 | + BrownfieldNavigationManager.shared.setDelegate( |
| 82 | + navigationDelegate: RNNavigationDelegate() |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Lifecycle Requirements |
| 89 | + |
| 90 | +- Register delegate before rendering JS that might call the module. |
| 91 | +- Keep navigation on main/UI thread. |
| 92 | +- Re-register delegate if your host object is recreated. |
| 93 | +- Treat missing delegate as a startup bug: runtime calls require a registered delegate. |
| 94 | + |
| 95 | +## Troubleshooting |
| 96 | + |
| 97 | +- **Method added in TS but not visible natively**: rerun codegen and rebuild. |
| 98 | +- **Calls crash on app launch**: verify delegate registration happens before RN route rendering. |
| 99 | +- **Wrong screen opens**: check native delegate method wiring and params mapping. |
0 commit comments