|
| 1 | +# iOS Development Guide |
| 2 | + |
| 3 | +## Architecture Overview |
| 4 | + |
| 5 | +``` |
| 6 | +packages/ios/ Swift Package |
| 7 | + ↓ loads at runtime |
| 8 | +packages/ios/Sources/Snapfill/Resources/ snapfill.js + snapfill-fill.js (generated from @snapfill/core) |
| 9 | +``` |
| 10 | + |
| 11 | +The iOS library loads the same injectable scripts that `@snapfill/react-native` uses. It creates a `window.ReactNativeWebView` shim that routes `postMessage` calls to a `WKScriptMessageHandler`, so the scripts work unchanged. |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +### Generate JavaScript Assets |
| 16 | + |
| 17 | +The JS assets must be generated from `@snapfill/core` before building: |
| 18 | + |
| 19 | +```bash |
| 20 | +pnpm install |
| 21 | +pnpm build # Build @snapfill/core |
| 22 | +pnpm generate:native # Generates snapfill.js + snapfill-fill.js into Resources/ |
| 23 | +``` |
| 24 | + |
| 25 | +### Swift Package Manager |
| 26 | + |
| 27 | +Add the package as a local dependency in Xcode or in your `Package.swift`: |
| 28 | + |
| 29 | +```swift |
| 30 | +.package(path: "path/to/packages/ios") |
| 31 | +``` |
| 32 | + |
| 33 | +Then add `Snapfill` to your target's dependencies. |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### Option A: Helper class (attach to any WKWebView) |
| 38 | + |
| 39 | +```swift |
| 40 | +import Snapfill |
| 41 | +import WebKit |
| 42 | + |
| 43 | +class CheckoutViewController: UIViewController, SnapfillDelegate { |
| 44 | + let webView = WKWebView() |
| 45 | + var snapfill: Snapfill! |
| 46 | + |
| 47 | + override func viewDidLoad() { |
| 48 | + super.viewDidLoad() |
| 49 | + |
| 50 | + snapfill = Snapfill(webView: webView) |
| 51 | + snapfill.delegate = self |
| 52 | + snapfill.attach() |
| 53 | + |
| 54 | + webView.load(URLRequest(url: URL(string: "https://example.com/checkout")!)) |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: - SnapfillDelegate |
| 58 | + |
| 59 | + func snapfillDidDetectFields(_ snapfill: Snapfill, fields: [String]) { |
| 60 | + print("Detected fields: \(fields)") |
| 61 | + } |
| 62 | + |
| 63 | + func snapfillDidDetectCart(_ snapfill: Snapfill, cart: SnapfillCart) { |
| 64 | + print("Cart total: \(cart.total) \(cart.currency ?? "")") |
| 65 | + } |
| 66 | + |
| 67 | + func snapfillDidCaptureValues(_ snapfill: Snapfill, mappings: [String: String]) { |
| 68 | + print("Captured: \(mappings)") |
| 69 | + } |
| 70 | + |
| 71 | + func snapfillDidCompleteFill(_ snapfill: Snapfill, result: SnapfillFillResult) { |
| 72 | + print("Filled \(result.filled)/\(result.total)") |
| 73 | + } |
| 74 | + |
| 75 | + func fillCheckout() { |
| 76 | + snapfill.fillForm([ |
| 77 | + "firstName": "John", |
| 78 | + "lastName": "Doe", |
| 79 | + "email": "john@example.com" |
| 80 | + ]) |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Option B: SnapfillWebView subclass |
| 86 | + |
| 87 | +```swift |
| 88 | +import Snapfill |
| 89 | + |
| 90 | +class CheckoutViewController: UIViewController, SnapfillDelegate { |
| 91 | + let webView = SnapfillWebView() |
| 92 | + |
| 93 | + override func viewDidLoad() { |
| 94 | + super.viewDidLoad() |
| 95 | + webView.snapfillDelegate = self |
| 96 | + webView.load(URLRequest(url: URL(string: "https://example.com/checkout")!)) |
| 97 | + } |
| 98 | + |
| 99 | + func snapfillDidDetectFields(_ snapfill: Snapfill, fields: [String]) { ... } |
| 100 | + |
| 101 | + func fillCheckout() { |
| 102 | + webView.fillForm(["email": "john@example.com"]) |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Bridge Mechanism |
| 108 | + |
| 109 | +1. **`WKScriptMessageHandler`** — `userContentController.add(self, name: "snapfill")` registers a message handler |
| 110 | +2. **Bridge shim** — injected as `WKUserScript` at `.atDocumentStart`: `window.ReactNativeWebView={postMessage:function(m){webkit.messageHandlers.snapfill.postMessage(m);}};` |
| 111 | +3. **Detection scripts** — injected as `WKUserScript` at `.atDocumentEnd` |
| 112 | +4. **Messages** — parsed from JSON and dispatched to `SnapfillDelegate` on the main thread |
| 113 | +5. **Fill** — `webView.evaluateJavaScript()` injects the fill script with mappings |
| 114 | + |
| 115 | +## API Reference |
| 116 | + |
| 117 | +### `Snapfill` |
| 118 | + |
| 119 | +| Method | Description | |
| 120 | +|---|---| |
| 121 | +| `attach()` | Adds user scripts and message handler to the WKWebView | |
| 122 | +| `fillForm(_:)` | Injects a fill script with the given field mappings | |
| 123 | +| `reinject()` | Re-injects bridge shim and detection scripts | |
| 124 | +| `detach()` | Removes all user scripts and the message handler | |
| 125 | + |
| 126 | +### `SnapfillDelegate` |
| 127 | + |
| 128 | +All methods are optional via default extensions. |
| 129 | + |
| 130 | +| Callback | Trigger | |
| 131 | +|---|---| |
| 132 | +| `snapfillDidDetectFields(_:fields:)` | Form fields detected on the page | |
| 133 | +| `snapfillDidDetectCart(_:cart:)` | Shopping cart data extracted | |
| 134 | +| `snapfillDidCaptureValues(_:mappings:)` | User-entered values captured | |
| 135 | +| `snapfillDidCompleteFill(_:result:)` | Form fill operation completed | |
| 136 | + |
| 137 | +### `SnapfillOptions` |
| 138 | + |
| 139 | +| Field | Default | Description | |
| 140 | +|---|---|---| |
| 141 | +| `detectForms` | `true` | Enable form field detection | |
| 142 | +| `detectCart` | `true` | Enable cart detection | |
| 143 | +| `captureValues` | `true` | Enable value capture | |
| 144 | + |
| 145 | +## Running Tests |
| 146 | + |
| 147 | +```bash |
| 148 | +cd packages/ios |
| 149 | +swift build |
| 150 | +swift test |
| 151 | +``` |
| 152 | + |
| 153 | +## Key Design Decisions |
| 154 | + |
| 155 | +**No external dependencies**: The library uses only the `WebKit` framework (part of iOS SDK). |
| 156 | + |
| 157 | +**Main thread dispatch**: All `SnapfillDelegate` callbacks are dispatched on the main thread via `DispatchQueue.main.async`. |
| 158 | + |
| 159 | +**`Bundle.module` for resources**: Swift Package Manager's resource processing creates a `Bundle.module` accessor for the generated JS files. |
| 160 | + |
| 161 | +**Min iOS 15**: Required for stable `WKUserScript` injection timing and `async/await` availability in the platform. |
| 162 | + |
| 163 | +**Sendable conformance**: All model structs conform to `Sendable` for safe use with Swift concurrency. |
0 commit comments