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

2023-09-08移动开发问题
9

本文介绍了在 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 之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Cocos2D 2.1 及宽后缀
Cocos2D 2.1 and wide suffix(Cocos2D 2.1 及宽后缀)...
2024-08-11 移动开发问题
2

无法将文件从捆绑包复制到 iOS 中的文档目录
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)...
2024-04-15 移动开发问题
4

如何复制“字典"在斯威夫特?
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)...
2024-04-15 移动开发问题
10

Swift - 迭代结构对象时如何对其进行变异
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)...
2024-04-15 移动开发问题
7

如何使用 Swift 将文本复制到剪贴板/粘贴板
How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)...
2024-04-15 移动开发问题
9

Swift 无法使用类型为“([Score],Score)"的参数列表调用“find",其中 Sco
Swift Cannot invoke #39;find#39; with an argument list of type #39;([Score], Score)#39; where Score is a struct(Swift 无法使用类型为“([Score],Score)的参数列表调用“find,其中 Score 是一个结构)...
2024-04-15 移动开发问题
6