简体   繁体   English

如何在Apple Watch上存储本地设置?

[英]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. 我知道这里列出的方法可以在Apple Watch和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. 但是,他们提到无法在Apple Watch端更改设置,并且需要WCSession才能从手表更改设置。

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. 我的最终目标是,一旦用户在手表上更改了Apple 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). 编辑:从那以后,我意识到,即使Apple不鼓励在手表上进行偏好设置,也完全有可能(可以像在iOS中那样完全使用UserDefaults)。 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. 然后,如果需要在手机和手表之间传输设置,则手表连接性(特别是TransferUserInfo)可以完成这项工作。

UserDefaults is just a file-backed dictionary. UserDefaults只是一个文件支持的字典。 The file is stored as a plist and UserDefaults is essentially built on top of PropertyListSerialization. 该文件存储为plist,而UserDefaults本质上是建立在PropertyListSerialization之上的。 For a simple setup, you can roll your own defaults. 对于简单的设置,您可以滚动自己的默认值。 File storage on the watch is basically the same as in iOS: 手表上的文件存储与iOS中的基本相同:

Data placement. 数据放置。 WatchKit extensions must take an active role in managing your data. WatchKit扩展必须在管理数据中发挥积极作用。 The container directory for your WatchKit extension has the same basic structure as the container for your iOS app. WatchKit扩展的容器目录具有与iOS应用程序容器相同的基本结构。 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. 尽可能将文件放在Caches目录中,以便在可用磁盘空间不足时由系统删除它们。

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 . 要共享首选项,您可以在此处使用解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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