To test the proposal, I have created a small sample project.
I use this code in the iOS/watchOS app.
import SwiftUI
import Foundation
struct ContentView: View {
@State var text: String = ""
init() {
let store = NSUbiquitousKeyValueStore.default
store.synchronize()
if let savedText = getDataFromiCloud(key: "text") {
_text = .init(initialValue: savedText)
}
}
var body: some View {
VStack {
TextField("Input", text: $text)
.padding()
Button("Submit") {
saveDataToiCloud(key: "text", value: text)
print("saved!")
}
}
.padding()
}
func saveDataToiCloud(key: String, value: String) {
let store = NSUbiquitousKeyValueStore.default
store.set(value, forKey: key)
store.synchronize()
}
func getDataFromiCloud(key: String) -> String? {
let store = NSUbiquitousKeyValueStore.default
return store.string(forKey: key)
}
}
#Preview {
ContentView()
}
In both targets, iCloud Key-value storage capability is enabled.
In recommendations function I try to read the data.
func recommendations() -> [AppIntentRecommendation<ConfigurationAppIntent>] {
let descriptionFromICloud = NSUbiquitousKeyValueStore.default.string(forKey: "text")
let intent = ConfigurationAppIntent()
let recommendation = AppIntentRecommendation(intent: intent, description: descriptionFromICloud ?? "no data")
return [recommendation]
}
Now I have two problems:
the data is not synchronized between iOS and watchOS app.
the data is also not available in recommendations function, I always get “no data” as widget description.
What am I doing wrong?
I would be very thankful for any support.
Post
Replies
Boosts
Views
Activity
I have a new insight. If I start the widget extension for watch in Xcode manually and debug it, it works. After that it doesn't work anymore, no matter if I restart apple watch or do something else, as if the recommendations function is no longer called.