Skip to content

Commit

Permalink
Merge pull request #61 from hotwired/document-new-global-config
Browse files Browse the repository at this point in the history
Document new global config
  • Loading branch information
jayohms authored Dec 18, 2024
2 parents 980c6d2 + a41faab commit a2f830f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
24 changes: 17 additions & 7 deletions _source/ios/02_path_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,26 @@ This configuration does two things:

Path configuration has an array of `sources`. You can configure the source to be a locally bundled file, a remote file available from your server, or both. We recommend always including a bundled version even when loading remotely, so it will be available in case your app is offline.

Set up your path configuration in `AppDelegate.swift`. This ensures that Hotwire is configured before the first URL is routed.

```swift
let localPathConfigURL = Bundle.main.url(forResource: "path-configuration", withExtension: "json")!
let remotePathConfigURL = URL(string: "https://example.com/configurations/ios_v1.json")!
import HotwireNative
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let localPathConfigURL = Bundle.main.url(forResource: "path-configuration", withExtension: "json")!
let remotePathConfigURL = URL(string: "https://example.com/configurations/ios_v1.json")!

let pathConfiguration = PathConfiguration(sources: [
.file(localPathConfigURL),
.server(remotePathConfigURL)
])
Hotwire.loadPathConfiguration(from: [
.file(localPathConfigURL),
.server(remotePathConfigURL)
])

let navigator = Navigator(pathConfiguration: pathConfiguration)
return true
}
}
```

If you provide both a file and a server location, the path configuration will be loaded asynchronously in the following order:
Expand Down
17 changes: 13 additions & 4 deletions _source/ios/03_bridge_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,21 @@ First, the component identifies itself as `"button"` via `name` to match the Sti

`onReceive(message:)` is called when a message is received from Stimulus. Here, the `{title}` object is unpacked to add a native button to the right side of the screen. When it's tapped, the `UIAction` is fired, replying to the message and calling the callback block, clicking the button.

Finally, register the component. If you followed the [getting started steps](/ios/getting-started) then this will go in `SceneDelegate.swift` before routing your first URL.
Finally, register the component in `AppDelegate.swift`. This ensures that Hotwire is configured before the first URL is routed.

```swift
Hotwire.registerBridgeComponents([
ButtonComponent.self
])
import HotwireNative
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Hotwire.registerBridgeComponents([
ButtonComponent.self
])
return true
}
}
```

## Add CSS to Hide Bridged Elements
Expand Down
51 changes: 34 additions & 17 deletions _source/ios/05_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,49 @@ description: "How to customize a Hotwire Native iOS app."

# Configuration

`HotwireConfig` provides a few options to customize your Hotwire Native iOS app. We recommend making all configuration changes *before* instantiating a `Navigator`, ideally in `AppDelegate` or `SceneDelegate`.

Append the following options to `Hotwire.config` to change the global configuration. For example, to enable debug logging call:

```swift
Hotwire.config.debugLoggingEnabled = true
```
Hotwire Native provides a few options to customize your iOS app. We recommend making all configuration changes *before* instantiating a `Navigator`, ideally in `AppDelegate.swift`.

## General

* `debugLoggingEnabled` - Enable or disable debug logging for Turbo visits and bridge elements connecting, disconnecting, receiving/sending messages, and more.
* `userAgent` - Override to set a custom user agent for your app's requests. Make sure to include "Hotwire Native" or "Turbo Native" to use `turbo_native_app?` on your [Rails server](https://github.com/hotwired/turbo-rails/blob/1aa7ba9d38dee1e1b4078a74404131122b907176/app/controllers/turbo/native/navigation.rb#L14).
* `showDoneButtonOnModals` - When enabled, adds a `UIBarButtonItem` of type `.done` to the left navigation bar button item on screens presented modally.
* `backButtonDisplayMode` - Sets the back button display mode of `HotwireWebViewController`.
* `Hotwire.config.debugLoggingEnabled` - Enable or disable debug logging for Turbo visits and bridge elements connecting, disconnecting, receiving/sending messages, and more.
* `Hotwire.config.userAgent` - Override to set a custom user agent for your app's requests. Make sure to include "Hotwire Native" or "Turbo Native" to use `turbo_native_app?` on your [Rails server](https://github.com/hotwired/turbo-rails/blob/1aa7ba9d38dee1e1b4078a74404131122b907176/app/controllers/turbo/native/navigation.rb#L14).
* `Hotwire.config.showDoneButtonOnModals` - When enabled, adds a `UIBarButtonItem` of type `.done` to the left navigation bar button item on screens presented modally.
* `Hotwire.config.backButtonDisplayMode` - Sets the back button display mode of `HotwireWebViewController`.

## Turbo

* `defaultViewController` - The view controller used in `Navigator` for web requests. Must be a `VisitableViewController` or subclass.
* `defaultNavigationController` - The navigation controller used in `Navigator` for the main and modal stacks. Must be a `UINavigationController` or subclass.
* `makeCustomWebView` - Optionally customize the web views used by each Turbo Session. Ensure you return a new instance each time.
* `Hotwire.config.defaultViewController` - The view controller used in `Navigator` for web requests. Must be a `VisitableViewController` or subclass.
* `Hotwire.config.defaultNavigationController` - The navigation controller used in `Navigator` for the main and modal stacks. Must be a `UINavigationController` or subclass.
* `Hotwire.config.makeCustomWebView` - Optionally customize the web views used by each Turbo Session. Ensure you return a new instance each time.

## Path Configuration

* `pathConfiguration.matchQueryStrings` - Enable to match the query string when applying rules in addition to the path.
* `Hotwire.config.pathConfiguration.matchQueryStrings` - Enable to match the query string when applying rules in addition to the path.

Load path configuration with `Hotwire.loadPathConfiguration(from:)`, like so:

```swift
let localPathConfigURL = Bundle.main.url(forResource: "path-configuration", withExtension: "json")!
let remotePathConfigURL = URL(string: "https://example.com/configurations/ios_v1.json")!

Hotwire.loadPathConfiguration(from: [
.file(localPathConfigURL),
.server(remotePathConfigURL)
])
```

## Bridge

* `jsonEncoder` - Set a custom JSON encoder when parsing bridge payloads. The custom encoder can be useful when you need to apply specific encoding strategies, like snake case vs. camel case.
* `jsonDecoder` - Set a custom JSON decoder when parsing bridge payloads. The custom decoder can be useful when you need to apply specific decoding strategies, like snake case vs. camel case.
* `Hotwire.config.jsonEncoder` - Set a custom JSON encoder when parsing bridge payloads. The custom encoder can be useful when you need to apply specific encoding strategies, like snake case vs. camel case.
* `Hotwire.config.jsonDecoder` - Set a custom JSON decoder when parsing bridge payloads. The custom decoder can be useful when you need to apply specific decoding strategies, like snake case vs. camel case.

Register bridge components with `Hotwire.registerBridgeComponents()`, like so:

```swift
Hotwire.registerBridgeComponents([
FormComponent.self,
MenuComponent.self,
OverflowMenuComponent.self,
// ...
])
```

0 comments on commit a2f830f

Please sign in to comment.