简体   繁体   中英

How can local settings be stored on apple watch?

I am aware of the methods listed here to save preferences across Apple Watch and iOS.

But, they mention that settings cannot be changed on the Apple Watch side, and that a WCSession would be needed to change settings from the watch.

I am looking for a method to store preferences locally on the watch. These preferences are just for the watch (so a shared preferences scheme is not what I'm looking for). Also, the method needs to work with or without the phone present.

My end goal is just to have switches on my Apple Watch app retain their state once the user changes them on the watch. I want their state to be retained if the app is closed and reopened.

Any ideas on how to do this? My only idea so far is to save a file locally to the watch and read from that on launch, but I feel like there must be a simpler way.

EDIT: I have since realized that even though Apple discourages the setting of preferences on the watch, it is completely possible (UserDefaults can be used EXACTLY as it is in iOS). This allowed me to have local watch settings. Then, if settings need to be transferred between the phone and the watch, Watch Connectivity (specifically, TransferUserInfo) can do the job.

UserDefaults is just a file-backed dictionary. The file is stored as a plist and UserDefaults is essentially built on top of PropertyListSerialization. For a simple setup, you can roll your own defaults. File storage on the watch is basically the same as in iOS:

Data placement. WatchKit extensions must take an active role in managing your data. The container directory for your WatchKit extension has the same basic structure as the container for your iOS app. Place user data and other critical information in the Documents directory. Place files in the Caches directory whenever possible so that they can be deleted by the system when the amount of free disk space is low.

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

guard let url = urls.first else {
    return
}

// The types that can be stored in this dictionary are the same as what NSUserDefaults can do
let preferences = [ "myDefault" : true ]

if let data = try? PropertyListSerialization.data(fromPropertyList: preferences, format: .xml, options: 1) {
    do {
        try data.write(to: url.appendingPathComponent("mypreferences.plist"))
    } catch {
        print("Failed to write")
    }
}

if let input = try? Data(contentsOf: url.appendingPathComponent("mypreferences.plist")),
    let dictionary = try? PropertyListSerialization.propertyList(from: input, options: .mutableContainersAndLeaves, format: nil),
    let d = dictionary as? [String : Bool],
    let value = d["myDefault"] {
        print(value)
}

Alternatively

To share the preferences, you can use the solution here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM