Custom URL Schemes

Goal

Register custom URL schemes for WebView-to-Unity communication (OAuth callbacks, deep linking, custom protocols).

Actions Required

Action
Purpose

WebViewCreateInstance

Creates WebView instance

WebViewAddURLScheme

Registers custom scheme

WebViewLoadURL

Loads web content

WebViewOnURLSchemeMatchFound

Listens for scheme matches

Variables Needed

  • webViewInstance (Object): Stores the WebView instance

  • customScheme (String): Custom scheme (e.g., "myapp", "unity", "oauth")

  • targetURL (String): URL to load

  • matchedURLScheme (String): The full URL that matched the scheme

Implementation Steps

1. State: CreateWebView

Action: WebViewCreateInstance

  • Outputs:

    • webViewInstance β†’ Store in FsmObject variable

  • Next: RegisterScheme

2. State: RegisterScheme

Action: WebViewAddURLScheme

  • Inputs:

    • webViewInstance β†’ From variable

    • urlScheme β†’ customScheme variable (e.g., "myapp")

  • Events:

    • successEvent β†’ LoadContent

    • failureEvent β†’ ShowError

Note: Register schemes BEFORE loading content that might trigger them.

3. State: LoadContent

Action: WebViewLoadURL

  • Inputs:

    • webViewInstance β†’ From variable

    • url β†’ targetURL variable

  • Next: ShowWebView

4. State: ShowWebView

Action: WebViewShow

  • Inputs:

    • webViewInstance β†’ From variable

  • Events:

    • successEvent β†’ Next

    • failureEvent β†’ ShowError

Action: WebViewOnURLSchemeMatchFound

  • Inputs:

    • webViewInstance β†’ From variable (optional filter)

  • Outputs:

    • matchedURLScheme β†’ Store in an FsmString variable

  • Events:

    • onMatchEvent β†’ ProcessCallback

Pattern: This listener stays active until a matching URL is clicked.

6. State: ProcessCallback

Parse matchedURLScheme for parameters

  • Handle OAuth tokens, deep link data, etc.

Common Issues

  • Scheme not recognized: Ensure scheme is registered BEFORE loading URL

  • Wrong URL format: Schemes must be lowercase, format: scheme://path?params

  • Multiple WebViews: Use instance filter on WebViewOnURLSchemeMatchFound

  • Parameters not parsing: URL decode parameters before using

Flow Diagram

Best Practices

  • Use lowercase scheme names (e.g., "myapp", not "MyApp")

  • Register all schemes before loading content

  • URL encode parameters in web links

  • URL decode parameters when processing in Unity

  • Use descriptive paths: myapp://login/success vs myapp://callback

  • Handle errors gracefully (invalid tokens, missing parameters)

Example Configurations

OAuth Callback

Deep Linking

Payment Confirmation

Multi-Action Callbacks

URL Parameter Parsing

Example JavaScript for website to construct callback URLs:

Example Unity processing (in ProcessCallback state):

Security Considerations

  • Validate all parameters from callback URLs (don't trust client data)

  • Use HTTPS for OAuth flows (protect tokens in transit)

  • Implement CSRF protection for sensitive actions

  • Verify OAuth state parameters to prevent replay attacks

  • Never include secrets in callback URLs (use server-side validation)

Advanced: Multiple Schemes

Register multiple schemes for different purposes:

Last updated

Was this helpful?