OpenURLAction function creates an environment action that intercepts URL opening requests, allowing you to implement custom URL handling behavior instead of the default system behavior.
Parameters
callback- Function that receives a URL string and returns anOpenURLActionResultor void
Return Types
The callback can return one of theseOpenURLActionResult types:
{ handled: true }
Indicates that the URL was handled by your custom logic and should not be processed further.
{ discarded: true }
Indicates that the URL opening request should be discarded/cancelled entirely.
{ systemAction: true }
Allows the system to handle the URL using default behavior.
{ systemAction: { url: string, preferInApp?: boolean } }
Allows the system to handle the URL, but with a potentially modified URL and optional in-app preference.
Usage
Basic URL interception
Custom URL handling
URL validation and blocking
URL rewriting
Analytics tracking
Conditional handling
Deep link routing
Notes
- The action is passed to child components through the environment system
- Child components can access it via
useEnvironment().openURL(url, callback) - Returning
void(no return value) is treated the same as{ systemAction: true } - The
preferInAppoption suggests opening the URL in an in-app browser when available - Platform support for
preferInAppvaries - some platforms always open externally - URL validation is your responsibility - the system doesn’t validate URLs before calling your handler
- For security, always validate and sanitize URLs before processing them
- The handler is synchronous - use state updates or side effects for async operations
- Custom schemes (like
myapp://) are commonly used for deep linking - This is particularly useful for implementing navigation, analytics, and security policies
