71 lines
2.3 KiB
Markdown
71 lines
2.3 KiB
Markdown
# SecureStorageSample Watch App
|
|
|
|
A watchOS companion app demonstrating data synchronization with the iOS app using WatchConnectivity.
|
|
|
|
## Overview
|
|
|
|
This watch app receives `UserProfile` data and the syncable setting from the paired iPhone via WatchConnectivity. It does **not** use LocalData directly for storage—instead, it displays synced data in memory.
|
|
|
|
## Architecture
|
|
|
|
```
|
|
SecureStorageSample Watch App/
|
|
├── ContentView.swift # Displays synced profile data
|
|
├── SecureStorageSampleApp.swift
|
|
├── Design/
|
|
│ └── WatchDesignConstants.swift
|
|
├── Protocols/
|
|
│ └── WatchDataHandling.swift # Protocol for payload handlers
|
|
├── State/
|
|
│ └── WatchProfileStore.swift # Holds synced profile in memory
|
|
└── Services/
|
|
├── WatchConnectivityService.swift
|
|
└── Handlers/
|
|
└── UserProfileWatchHandler.swift
|
|
└── SyncableSettingWatchHandler.swift
|
|
```
|
|
|
|
## Data Flow
|
|
|
|
1. **Watch app** requests a sync when it launches or becomes reachable
|
|
2. **iOS app** replies with a snapshot of syncable keys and updates `applicationContext`
|
|
3. **Watch app** receives context in `WatchConnectivityService`
|
|
4. The service dispatches each payload key to its registered `WatchDataHandling` handler
|
|
5. Handlers decode values and update `WatchProfileStore`
|
|
|
|
## Launch-Order-Safe Sync
|
|
|
|
The watch app handles both cases:
|
|
|
|
- If the iPhone is reachable, it sends a `request_sync` message and applies the reply payload.
|
|
- If the iPhone is not reachable, it queues a request with `transferUserInfo` and shows a badge.
|
|
|
|
This ensures users do not need to launch the apps in a specific order.
|
|
|
|
## Adding New Sync Payloads
|
|
|
|
1. Create a new handler conforming to `WatchDataHandling`:
|
|
|
|
```swift
|
|
struct MyDataWatchHandler: WatchDataHandling {
|
|
let key = "myData"
|
|
|
|
func handle(data: Data) {
|
|
// Decode and update state
|
|
}
|
|
}
|
|
```
|
|
|
|
2. Register it in `WatchConnectivityService.registerDefaultHandlers()`
|
|
|
|
## Limitations
|
|
|
|
- **No persistent storage on watch**: Data is held in memory only
|
|
- **One-way sync**: Watch receives data from iPhone; it does not send data back
|
|
- **Simulator limitations**: WatchConnectivity may not work reliably in the simulator
|
|
|
|
## Requirements
|
|
|
|
- watchOS 10.0+
|
|
- Paired with iOS app via WatchConnectivity
|