From ee7a4965a44e80be32a15c8fde6f33dde95e6a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E9=93=AD?= Date: Thu, 9 May 2024 20:43:10 +0800 Subject: [PATCH] add widget swiftdata --- SwiftPamphletApp.xcodeproj/project.pbxproj | 12 ++++++++++++ SwiftPamphletApp/Guide/GuideListView.swift | 4 +++- .../Widget\350\256\277\351\227\256SwiftData(ap).md" | 12 ++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 "SwiftPamphletApp/Resource/Guide/Widget\345\260\217\351\203\250\344\273\266/Widget\350\256\277\351\227\256SwiftData(ap).md" diff --git a/SwiftPamphletApp.xcodeproj/project.pbxproj b/SwiftPamphletApp.xcodeproj/project.pbxproj index 671247128..9d72b20b0 100644 --- a/SwiftPamphletApp.xcodeproj/project.pbxproj +++ b/SwiftPamphletApp.xcodeproj/project.pbxproj @@ -205,6 +205,7 @@ 08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */ = {isa = PBXBuildFile; productRef = 08D107BC278826BB007B7009 /* HTMLEntities */; }; 08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = 08ED80152B9C54DE0069B7EC /* SMNetwork */; }; 08ED801C2B9D1EEC0069B7EC /* SettingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08ED801B2B9D1EEC0069B7EC /* SettingView.swift */; }; + 08EF35CD2BECF3120098E2D4 /* Widget访问SwiftData(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08EF35CC2BECF3120098E2D4 /* Widget访问SwiftData(ap).md */; }; 08F14B3C2BBDA3EA005B46CC /* Nuke in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3B2BBDA3EA005B46CC /* Nuke */; }; 08F14B3E2BBDA3EA005B46CC /* NukeExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3D2BBDA3EA005B46CC /* NukeExtensions */; }; 08F14B402BBDA3EA005B46CC /* NukeUI in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B3F2BBDA3EA005B46CC /* NukeUI */; }; @@ -421,6 +422,7 @@ 08CD61FC27758B8A008C0935 /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = ""; }; 08CD61FD27758B8A008C0935 /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = ""; }; 08ED801B2B9D1EEC0069B7EC /* SettingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingView.swift; sourceTree = ""; }; + 08EF35CC2BECF3120098E2D4 /* Widget访问SwiftData(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Widget访问SwiftData(ap).md"; sourceTree = ""; }; 08F14B432BBE2865005B46CC /* ViewComponentImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponentImage.swift; sourceTree = ""; }; 08F51BC427A374A500693AB6 /* footer_js.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = footer_js.html; sourceTree = ""; }; 3AE0D5992BAB0A0600D6D925 /* DeveloperListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperListView.swift; sourceTree = ""; }; @@ -996,6 +998,7 @@ 08448F67279EB40D00B61353 /* 基础库 */, 08448F47279E7E3A00B61353 /* SwiftUI */, 08659BCB2BE9A3E6009B7C00 /* SwiftData */, + 08EF35CB2BECF1690098E2D4 /* Widget小部件 */, 08026C312869990100792EF1 /* 系统能力 */, 08448F7C279EB6F700B61353 /* 工程模式 */, 08EF35CA2BECF0B70098E2D4 /* 多线程 */, @@ -1049,6 +1052,14 @@ path = "../多线程"; sourceTree = ""; }; + 08EF35CB2BECF1690098E2D4 /* Widget小部件 */ = { + isa = PBXGroup; + children = ( + 08EF35CC2BECF3120098E2D4 /* Widget访问SwiftData(ap).md */, + ); + path = "Widget小部件"; + sourceTree = ""; + }; 08F1AB632BA083F500AEA0CA /* Info */ = { isa = PBXGroup; children = ( @@ -1323,6 +1334,7 @@ 08BE635427C63828002BC6A8 /* List(ap).md in Resources */, 08448FEA279EC86700B61353 /* 运算符(ap).md in Resources */, 08449006279ECB4900B61353 /* merge(ap).md in Resources */, + 08EF35CD2BECF3120098E2D4 /* Widget访问SwiftData(ap).md in Resources */, 08448FB1279EC33E00B61353 /* 类型转换(ap).md in Resources */, 08448FE6279EC82500B61353 /* 逻辑(ap).md in Resources */, 08BE636027C7A674002BC6A8 /* Navigation(ap).md in Resources */, diff --git a/SwiftPamphletApp/Guide/GuideListView.swift b/SwiftPamphletApp/Guide/GuideListView.swift index a9a7939ed..3a361d2eb 100644 --- a/SwiftPamphletApp/Guide/GuideListView.swift +++ b/SwiftPamphletApp/Guide/GuideListView.swift @@ -213,7 +213,9 @@ final class GuideListModel { L(t: "SwiftData-调试"), L(t: "SwiftData-资料") ]), - L(t: "Widget 小部件") + L(t: "Widget小部件", sub: [ + L(t: "Widget访问SwiftData") + ]), L(t: "系统能力",sub: [ L(t: "Swift-DocC") ]), diff --git "a/SwiftPamphletApp/Resource/Guide/Widget\345\260\217\351\203\250\344\273\266/Widget\350\256\277\351\227\256SwiftData(ap).md" "b/SwiftPamphletApp/Resource/Guide/Widget\345\260\217\351\203\250\344\273\266/Widget\350\256\277\351\227\256SwiftData(ap).md" new file mode 100644 index 000000000..736c87af7 --- /dev/null +++ "b/SwiftPamphletApp/Resource/Guide/Widget\345\260\217\351\203\250\344\273\266/Widget\350\256\277\351\227\256SwiftData(ap).md" @@ -0,0 +1,12 @@ + +Wdiget target 访问主应用 target 的 SwiftData 数据步骤如下: + +- 对主应用和 Widget 的 target 中的 Signing & Capabilities 都添加 App Groups,并创建一个新组,名字相同。 +- SwiftData 的模型同时在主应用和 Widget 的 target 中。 +- StaticConfiguration 或 AppIntentConfiguration 中添加 `modelContainer()` 修饰符,让 SwiftData 的容器可用。 + +SwiftData 数据变化后,小组件能够及时更新的方法 + +```swift +WidgetCenter.shared.reloadAllTimelines() +```