简体   繁体   English

使iOS和watchOS应用程序可以访问SwiftyUserDefaults数组

[英]Make SwiftyUserDefaults array accessible to iOS and watchOS app

I am using the excellent SwiftyUserDefaults , a wrapper for NSUserDefaults with Swift to allow my app to persist a certain array of data, like so (lots of code removed to show just the important parts: 我正在使用优秀的SwiftyUserDefaults ,一个NSUserDefaults与Swift的包装器,允许我的应用程序保留一定数量的数据,就像这样(删除了大量代码以显示重要部分:

Working Code 工作守则

 import SwiftyUserDefaults //The following is to show implementation in the parent iOS app. It works fine.
    class ViewController: UICollectionViewController { 

    var arrayToStore = DefaultsKey<NSArray>("arrayToStore")

    override func viewDidLoad() {
         Defaults[arrayToStore] = ["Element 1", "Element 2", "Element 3"]
        }
     func aFunctionThatNeedsToReadTheArray() {
         print([Defaults[arrayToStore]])
         }
    }

Research 研究

The above works fine, but I also want the watchKit watchOS 2 app of this parent app to be able to read arrayToStore. 以上工作正常,但我也希望这个父应用程序的watchKit watchOS 2应用程序能够读取arrayToStore。

I did a bit of research and learned that Apple offers a solution for this, App Groups, which enables things in NSUserDefaults and other data storage means to be available across apps - in this case, between a parent iOS app and its companion watchOS watchKit app. 我做了一些研究并了解到Apple为这个App Groups提供了一个解决方案,它可以在NSUserDefaults和其他数据存储方式中实现跨应用程序 - 在这种情况下,在父iOS应用程序和它的配套watchOS watchKit应用程序之间。 EDIT: I LEARNED THANKS TO @PETAHCHRISTIAN THAT APP GROUPS DOES NOT WORK FOR WATCHOS 2. SO, WHAT OTHER MEANS ARE THERE? 编辑:我得知@PETAHCHRISTIAN APP组不适用于WATCHOS 2.那么,其他方法是什么?

Edit 2: Knowing that App Groups is not the solution, I did a bit more research and have determined that a possible solution is the WatchConnectivity framework. 编辑2:知道应用程序组不是解决方案,我做了一些研究,并确定可能的解决方案是WatchConnectivity框架。 Is this correct, and if so, how can I use it to allow the watch app to have access to arrayToStore ? 这是否正确,如果是这样,我如何使用它来允许监视应用程序访问arrayToStore

Question

However, my question is: How do I do this with SwiftyUserDefaults ? 但是,我的问题是: 如何使用SwiftyUserDefaults执行此SwiftyUserDefaults I want to be able to have arrayToStore , an array in the parent app, readable by the watchOS 2 app via app groups (or another means, if there is a better way NOTE: Thanks to @PetahChristian I have learned that App Groups is not the way to do this. As such, how can I obtain the desired result of the watch app reading an array from the parent app via other means?). 我希望能够拥有arrayToStore ,即父应用程序中的数组,可通过应用程序组通过watchOS 2应用程序读取(或者其他方式,如果有更好的方式注意:感谢@PetahChristian我已经了解到应用程序组不是这样做的方法。如何,我如何通过其他方式从父应用程序获取手表应用程序读取数组的预期结果?)。

What I am trying: 我在想什么:

  1. Enable app groups by, in "Capabilities" of the iOS app and watchKit app, turning on app groups and selecting the app group as shown in the answer here 通过启用应用组,在iOS应用和watchKit应用的“能力”,打开的应用程序组和选择所述应用程序组如图中的答案这里
  2. In the app extension for the watchKit app, using print([Defaults[arrayToStore]]) , resulting in "Use of unresolved identifier arrayToStore". 在watchKit应用程序的app扩展中,使用print([Defaults[arrayToStore]]) ,导致“使用未解析的标识符arrayToStore”。 The expected result is printing "Element 1", "Element 2", "Element 3" 预期的结果是打印“元素1”,“元素2”,“元素3”

Use WatchConnectivity 使用WatchConnectivity

You can send a request from the watch to the phone and then send something back to the watch. 您可以将手表的请求发送到手机,然后将某些内容发回手表。 I would store the array in a Singleton, and then use the sendMessage function of WatchConnectivity. 我将数组存储在Singleton中,然后使用WatchConnectivity的sendMessage函数。 This function works even if the iPhone App is in Background. 即使iPhone应用程序处于后台,此功能也能正常工作。

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

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