<tfoot id='VbG46'></tfoot>
  1. <small id='VbG46'></small><noframes id='VbG46'>

  2. <legend id='VbG46'><style id='VbG46'><dir id='VbG46'><q id='VbG46'></q></dir></style></legend>
      <bdo id='VbG46'></bdo><ul id='VbG46'></ul>

    <i id='VbG46'><tr id='VbG46'><dt id='VbG46'><q id='VbG46'><span id='VbG46'><b id='VbG46'><form id='VbG46'><ins id='VbG46'></ins><ul id='VbG46'></ul><sub id='VbG46'></sub></form><legend id='VbG46'></legend><bdo id='VbG46'><pre id='VbG46'><center id='VbG46'></center></pre></bdo></b><th id='VbG46'></th></span></q></dt></tr></i><div id='VbG46'><tfoot id='VbG46'></tfoot><dl id='VbG46'><fieldset id='VbG46'></fieldset></dl></div>

      在 iOS 14 的 SwiftUI 中,在主 App 和 Widget 之间共享数据

      时间:2023-09-08

            <tbody id='Jjg97'></tbody>
        • <legend id='Jjg97'><style id='Jjg97'><dir id='Jjg97'><q id='Jjg97'></q></dir></style></legend>

            <tfoot id='Jjg97'></tfoot>
            • <bdo id='Jjg97'></bdo><ul id='Jjg97'></ul>
              <i id='Jjg97'><tr id='Jjg97'><dt id='Jjg97'><q id='Jjg97'><span id='Jjg97'><b id='Jjg97'><form id='Jjg97'><ins id='Jjg97'></ins><ul id='Jjg97'></ul><sub id='Jjg97'></sub></form><legend id='Jjg97'></legend><bdo id='Jjg97'><pre id='Jjg97'><center id='Jjg97'></center></pre></bdo></b><th id='Jjg97'></th></span></q></dt></tr></i><div id='Jjg97'><tfoot id='Jjg97'></tfoot><dl id='Jjg97'><fieldset id='Jjg97'></fieldset></dl></div>

              <small id='Jjg97'></small><noframes id='Jjg97'>

                本文介绍了在 iOS 14 的 SwiftUI 中,在主 App 和 Widget 之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                @main
                struct ClockWidgetExt: Widget {
                    private let kind: String = "ClockWidgetExt"
                    
                    public var body: some WidgetConfiguration {
                        StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
                            HomeTestView()
                        }
                        .configurationDisplayName("My Widget")
                        .description("This is an example widget.")
                    }
                }
                

                如何将数据从我的主应用程序获取到小部件?

                How can I get data from my Main App to the widget?

                推荐答案

                您可以为您的 Widget 和 App 添加 AppGroup 功能(here 很好地解释了如何添加它).

                You can add the AppGroup capability for both your Widget and App (here is a very good explanation how to add it).

                代替

                UserDefaults.standard
                

                只需为您的 AppGroup 使用共享的 UserDefaults:

                just use the shared UserDefaults for your AppGroup:

                UserDefaults(suiteName: <your_app_group>)
                

                然后您可以按照this answer中的说明读取/写入数据.

                Then you can read/write data like explained in this answer.

                通过 AppGroup 授权,您可以访问共享文件容器:

                With the AppGroup entitlement you get access to the shared File Container:

                let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: <your_app_group>)!
                

                并访问这样的网址:

                let someFileURL = containerURL.appendingPathComponent("SomeFile.txt")
                

                然后你可以使用你的共享文件容器,就像这个答案中解释的那样:

                Then you can use your shared File Container like explained in this answer:

                • 如何通过iOS WidgetKit读取应用创建的文件?

                您也可以创建一个共享的 CoreData 容器:

                You can create a shared CoreData container as well:

                let storeURL = containerURL.appendingPathComponent("DataModel.sqlite")
                let description = NSPersistentStoreDescription(url: storeURL)
                
                let container = NSPersistentContainer(name: "DataModel")
                container.persistentStoreDescriptions = [description]
                container.loadPersistentStores { ... }
                

                然后你可以使用你的共享 CoreData 容器,就像这个答案中解释的那样:

                Then you can use your shared CoreData Container like explained in this answer:

                • 从 CoreData for iOS 14 小部件中获取数据

                这是一个 GitHub 存储库,其中包含不同的 Widget 示例,包括 App Group小部件.

                Here is a GitHub repository with different Widget examples including the App Group Widget.

                这篇关于在 iOS 14 的 SwiftUI 中,在主 App 和 Widget 之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在小部件中使用自定义字体? 下一篇:将位图动态放入小部件时失败的活页夹事务

                相关文章

                <tfoot id='GTE0S'></tfoot>
              • <small id='GTE0S'></small><noframes id='GTE0S'>

              • <legend id='GTE0S'><style id='GTE0S'><dir id='GTE0S'><q id='GTE0S'></q></dir></style></legend>
              • <i id='GTE0S'><tr id='GTE0S'><dt id='GTE0S'><q id='GTE0S'><span id='GTE0S'><b id='GTE0S'><form id='GTE0S'><ins id='GTE0S'></ins><ul id='GTE0S'></ul><sub id='GTE0S'></sub></form><legend id='GTE0S'></legend><bdo id='GTE0S'><pre id='GTE0S'><center id='GTE0S'></center></pre></bdo></b><th id='GTE0S'></th></span></q></dt></tr></i><div id='GTE0S'><tfoot id='GTE0S'></tfoot><dl id='GTE0S'><fieldset id='GTE0S'></fieldset></dl></div>

                    • <bdo id='GTE0S'></bdo><ul id='GTE0S'></ul>