| .. | ||
| Assets.xcassets | ||
| Protocols | ||
| Services | ||
| State | ||
| ContentView.swift | ||
| README.md | ||
| SecureStorageSample Watch App.entitlements | ||
| SecureStorageSampleApp.swift | ||
SecureStorageSample Watch App
A watchOS companion app demonstrating data synchronization with the iOS app using WatchConnectivity.
Overview
This watch app receives UserProfile data synced from the paired iPhone via WCSession.updateApplicationContext. 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
├── Protocols/
│ └── WatchDataHandling.swift # Protocol for payload handlers
├── State/
│ └── WatchProfileStore.swift # Holds synced profile in memory
└── Services/
├── WatchConnectivityService.swift
└── Handlers/
└── UserProfileWatchHandler.swift
Data Flow
- iOS app calls
SyncHelperwhen storing data withsyncPolicy: .automaticSmallor.manual SyncHelpersends data viaWCSession.updateApplicationContext- Watch app receives context in
WatchConnectivityService - The service dispatches each payload key to its registered
WatchDataHandlinghandler UserProfileWatchHandlerdecodes the profile and updatesWatchProfileStore
Adding New Sync Payloads
- Create a new handler conforming to
WatchDataHandling:
struct MyDataWatchHandler: WatchDataHandling {
let key = "myData"
func handle(data: Data) {
// Decode and update state
}
}
- 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