Skip to content

Commit

Permalink
add widget swiftdata
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed May 9, 2024
1 parent a5c5a73 commit ee7a496
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -421,6 +422,7 @@
08CD61FC27758B8A008C0935 /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
08CD61FD27758B8A008C0935 /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = "<group>"; };
08ED801B2B9D1EEC0069B7EC /* SettingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingView.swift; sourceTree = "<group>"; };
08EF35CC2BECF3120098E2D4 /* Widget访问SwiftData(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Widget访问SwiftData(ap).md"; sourceTree = "<group>"; };
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponentImage.swift; sourceTree = "<group>"; };
08F51BC427A374A500693AB6 /* footer_js.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = footer_js.html; sourceTree = "<group>"; };
3AE0D5992BAB0A0600D6D925 /* DeveloperListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperListView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -996,6 +998,7 @@
08448F67279EB40D00B61353 /* 基础库 */,
08448F47279E7E3A00B61353 /* SwiftUI */,
08659BCB2BE9A3E6009B7C00 /* SwiftData */,
08EF35CB2BECF1690098E2D4 /* Widget小部件 */,
08026C312869990100792EF1 /* 系统能力 */,
08448F7C279EB6F700B61353 /* 工程模式 */,
08EF35CA2BECF0B70098E2D4 /* 多线程 */,
Expand Down Expand Up @@ -1049,6 +1052,14 @@
path = "../多线程";
sourceTree = "<group>";
};
08EF35CB2BECF1690098E2D4 /* Widget小部件 */ = {
isa = PBXGroup;
children = (
08EF35CC2BECF3120098E2D4 /* Widget访问SwiftData(ap).md */,
);
path = "Widget小部件";
sourceTree = "<group>";
};
08F1AB632BA083F500AEA0CA /* Info */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 3 additions & 1 deletion SwiftPamphletApp/Guide/GuideListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
]),
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
```

0 comments on commit ee7a496

Please sign in to comment.