I decided to start practicing how to communicate data between iOS/iPad and WatchOS. It seems like it’s not syncing correctly because when I add or remove a person, my watch doesn’t update. I have two watches: one connected via Bluetooth and the other connected via cellular or Wi-Fi. When I create a new person on my iOS app, my WatchOS app doesn’t reflect any changes. I have four people in my iOS app, but the WatchOS app shows an empty database. I’ve tried researching everywhere online and even consulted AI, but I haven’t been able to solve the issue. What could I be doing wrong, or am I missing something?
iOS App – ContentView.swift
struct ContentView: View {
@Environment(.modelContext) private var modelContext
// Array to hold the people for display
@Query(sort: Person.name, order: .forward) private var people: [Person]
// Add a single random person
func addRandomPerson() {
let names = ["John", "Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah", "Isaac"]
// Select a random name
let name = names.randomElement()!
// Generate random data for birthday, height, and weight
let randomBirthday = Date(timeIntervalSince1970: TimeInterval.random(in: 315532800..<(315532800 + (10 * 365 * 24 * 60 * 60)))) // Random year between 1980 and 1990
let randomHeight = Double.random(in: 5.5...6.5) // Random height between 5.5 and 6.5 feet
let randomWeight = Int.random(in: 170...210) // Random weight between 170 and 210 lbs
// Create the new person
let person = Person(name: name,
birthday: randomBirthday,
height: randomHeight,
weight: randomWeight)
// Insert the person into the model context
modelContext.insert(person)
}
// Delete a person from the list
func deletePerson(at offsets: IndexSet) {
for index in offsets {
let person = people[index]
modelContext.delete(person)
}
}
var body: some View {
VStack {
Button("Add Random Person") {
addRandomPerson() // Add a single random person
}
.padding()
List {
ForEach(people) { person in
VStack(alignment: .leading) {
Text(person.name)
.font(.headline)
Text("Birthday: (person.birthday.formatted(.dateTime.month().day().year()))")
.font(.subheadline)
Text("Height: (person.height, specifier: "%.1f")")
Text("Weight: (person.weight)")
}
.onTapGesture {
// Handle edit functionality if needed
}
}
.onDelete(perform: deletePerson)
}
}
.padding()
}
}
iOS App – SwiftData.swift
@Model
class Person {
var name: String
var birthday: Date
var height: Double
var weight: Int
init(name: String, birthday: Date, height: Double, weight: Int) {
self.name = name
self.birthday = birthday
self.height = height
self.weight = weight
}
}
iOS App – Startup App
@main
struct Watch_ConnectionApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Person.self)
}
}
Watch App – ContentView.swift
struct PersonListView: View {
@Query(sort: Person.name, order: .forward) private var people: [Person]
public init() { }
var body: some View {
List(people) { person in
VStack(alignment: .leading) {
Text(person.name)
.font(.headline)
Text("Birthday: (person.birthday.formatted(.dateTime.month().day().year()))")
.font(.subheadline)
Text("Height: (person.height, specifier: "%.1f")")
Text("Weight: (person.weight)")
}
}
.navigationTitle("People")
}
}
Watch App – Startup App
@Model
class Person {
var name: String
var birthday: Date
var height: Double
var weight: Int
init(name: String, birthday: Date, height: Double, weight: Int) {
self.name = name
self.birthday = birthday
self.height = height
self.weight = weight
}
}
@main
struct Watch_Connection_App_Watch_AppApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
PersonListView()
}
}
.modelContainer(for: Person.self) // Add SwiftData stack
}
}
4
iCloud sync is not automatically enabled for SwiftData – You need to set it up.
Apple provides documentation, but in summary you need to:
- Add the iCloud capability to your app in Xcode
- Add the remote notification capability to your app in Xcode
- Add some code to create a CloudKit schema