{"id":1273,"date":"2020-12-11T15:18:44","date_gmt":"2020-12-11T07:18:44","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1273"},"modified":"2020-12-11T15:18:44","modified_gmt":"2020-12-11T07:18:44","slug":"ios14-widget%e5%b0%8f%e7%bb%84%e4%bb%b6%e5%bc%80%e5%8f%91%e5%ae%9e%e8%b7%b51-%e4%ba%86%e8%a7%a3widget","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1273","title":{"rendered":"iOS14 Widget\u5c0f\u7ec4\u4ef6\u5f00\u53d1\u5b9e\u8df51\u2014\u2014\u4e86\u89e3Widget"},"content":{"rendered":"\n<p><code>iOS14<\/code>\u5e26\u6765\u4e86\u65b0\u7684UI\u7ec4\u4ef6\uff1a<code>WidgetKit<\/code>\uff0c\u4eba\u79f0\u5c0f\u7ec4\u4ef6\uff08\u7528\u8fc7\u5b89\u5353\u7684\u90fd\u80fd\u7406\u89e3\uff09\uff0c\u8fd9\u8d27\u7684\u524d\u8eab\u5176\u5b9e\u5c31\u662f<code>iOS10<\/code>\u65f6\u5019\u5f15\u5165\u7684<code>Today Extension<\/code>\u3002<\/p>\n\n\n\n<p><code>Widget<\/code>\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u8fd9\u6837\u4e00\u79cd\u529f\u80fd\uff1a\u5176\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u4e3b\u5c4f\u5e55\u4e0a\u5c55\u793aApp\u4e2d\u7528\u6237\u6240\u5173\u5fc3\u7684\u4fe1\u606f\u3002\u4f8b\u5982\u4e00\u6b3e\u5929\u6c14\u8f6f\u4ef6\uff0c\u5176\u53ef\u4ee5\u9644\u5e26\u4e00\u4e2a<code>Widget<\/code>\u8ba9\u7528\u6237\u5728\u4e3b\u5c4f\u5e55\u5c31\u53ef\u67e5\u770b\u4eca\u65e5\u7684\u5929\u6c14\u60c5\u51b5\uff0c\u4f8b\u5982\u80a1\u7968\u76f8\u5173\u7684\u8f6f\u4ef6\uff0c\u7528\u6237\u5c06\u81ea\u5df1\u611f\u5174\u8da3\u7684\u80a1\u7968\u6536\u85cf\uff0c\u65e0\u9700\u6253\u5f00App\uff0c\u5728\u4e3b\u5c4f\u5e55\u5373\u53ef\u67e5\u5230\u5bf9\u5e94\u7684\u80a1\u4ef7\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u521a\u597diOS14\u6b63\u5f0f\u7248\u66f4\u65b0\u7684\u8282\u594f\uff0c\u62bd\u70b9\u65f6\u95f4\u6765\u8bd5\u8bd5\u6574\u4e00\u4e2a<code>Widget<\/code>\u5c0f\u7ec4\u4ef6\u73a9\u73a9\u3002<\/p>\n\n\n\n<p><strong><code>Widget<\/code>\u53ea\u652f\u6301<code>SwiftUI<\/code>\u5b9e\u73b0\uff0c<code>OC<\/code>\u9879\u76ee\u540c\u6837\u53ef\u4ee5\u521b\u5efa\uff0c\u4f46\u662f\u5fc5\u987b\u7528<code>SwiftuUI<\/code>\u5b9e\u73b0\u3002<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa<\/h4>\n\n\n\n<p>\u901a\u8fc7<code>Xcode<\/code>\u00a0->\u00a0<code>File<\/code>\u00a0->\u00a0<code>New<\/code>\u00a0->\u00a0<code>Target<\/code>\u83dc\u5355\u8def\u5f84\u627e\u5230\u00a0<code>Widget Extension<\/code>\uff0c\u53cc\u51fb\u521b\u5efa<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-1-1024x743.png\" alt=\"\" class=\"wp-image-1274\" width=\"558\" height=\"404\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-1024x743.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-300x218.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-768x557.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-830x602.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-230x167.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-350x254.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1-480x348.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-1.png 1326w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u91cc\u968f\u4fbf\u8f93\u5165\u4f60\u81ea\u5df1\u7684\u547d\u540d\uff0c<code>Include Configuration Intent<\/code>\u8fd9\u9009\u9879\u6682\u65f6\u4e0d\u52fe\u9009\uff0c\u8fd9\u9009\u9879\u4e3b\u8981\u662f\u7528\u6765\u652f\u6301\u4f60\u81ea\u5b9a\u4e49\u4e00\u4e9b\u5c5e\u6027\u914d\u7f6e\uff08\u4f8b\u5982\u5929\u6c14\u7ec4\u4ef6\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u57ce\u5e02\uff0c\u80a1\u7968\u7ec4\u4ef6\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4ee3\u7801\uff09\uff0c\u4e0d\u652f\u6301\u7684\u8bdd\u5219\u4e0d\u7528\u52fe\u9009\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-2-1024x741.png\" alt=\"\" class=\"wp-image-1275\" width=\"572\" height=\"413\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-1024x741.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-300x217.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-768x556.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-830x601.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-230x166.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-350x253.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2-480x347.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-2.png 1324w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e4b\u540e\u5728<code>Xcode<\/code>\u7684\u5de6\u4fa7\u9879\u76ee\u76ee\u5f55\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u81ea\u52a8\u65b0\u589e\u4e86\u4e00\u4e9b\u4e1c\u897f\uff0c\u5176\u4e2d<code>TestWidget.swift<\/code>\u6587\u4ef6\u5c31\u662f\u6211\u4eec\u5373\u5c06\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-4-759x1024.png\" alt=\"\" class=\"wp-image-1277\" width=\"357\" height=\"481\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-759x1024.png 759w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-222x300.png 222w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-768x1036.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-830x1119.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-230x310.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-350x472.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4-480x647.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-4.png 964w\" sizes=\"(max-width: 357px) 100vw, 357px\" \/><\/figure><\/div>\n\n\n\n<p>\u5148\u70b9\u8fdb\u53bb\u770b\u4e0b<code>Xcode<\/code>\u4e3a\u6211\u4eec\u751f\u4ea7\u7684\u9ed8\u8ba4\u4ee3\u7801\uff0c\u7136\u540e\u8fd0\u884c\u8dd1\u4e00\u4e0b\uff0c\u770b\u770b\u6a21\u62df\u5668\u6548\u679c\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-6-494x1024.png\" alt=\"\" class=\"wp-image-1279\" width=\"330\" height=\"684\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6-494x1024.png 494w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6-145x300.png 145w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6-230x477.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6-350x725.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6-480x995.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-6.png 604w\" sizes=\"(max-width: 330px) 100vw, 330px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-8-495x1024.png\" alt=\"\" class=\"wp-image-1281\" width=\"335\" height=\"693\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8-495x1024.png 495w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8-145x300.png 145w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8-230x476.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8-350x724.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8-480x993.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-8.png 604w\" sizes=\"(max-width: 335px) 100vw, 335px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u8bfb<\/h4>\n\n\n\n<p>\u63a5\u4e0b\u6765\u770b\u4e0b<code>Xcode<\/code>\u751f\u6210\u7684\u9ed8\u8ba4<code>Widget<\/code>\u5b9e\u73b0\u4ee3\u7801<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Provider<\/h5>\n\n\n\n<p><code>Provider<\/code>\uff1a\u4e3a\u5c0f\u7ec4\u4ef6\u5c55\u793a\u63d0\u4f9b\u4e00\u5207\u5fc5\u8981\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\uff0c\u5b9e\u73b0<code>TimelineProvider<\/code>\u534f\u8bae<br><code>placeholder<\/code>\uff1a\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u89c6\u56fe\uff0c\u5f53\u7f51\u7edc\u6570\u636e\u8bf7\u6c42\u5931\u8d25\u6216\u8005\u5176\u4ed6\u4e00\u4e9b\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u7528\u4e8e\u5c55\u793a<br><code>getSnapshot<\/code>\uff1a\u4e3a\u4e86\u5728\u5c0f\u90e8\u4ef6\u5e93\u4e2d\u663e\u793a\u5c0f\u90e8\u4ef6\uff0c<code>WidgetKit<\/code>\u8981\u6c42\u63d0\u4f9b\u8005\u63d0\u4f9b\u9884\u89c8\u5feb\u7167\uff0c\u5728\u7ec4\u4ef6\u7684\u6dfb\u52a0\u9875\u9762\u53ef\u4ee5\u770b\u5230\u6548\u679c<br><code>getTimeline<\/code>\uff1a\u5728\u8fd9\u4e2a\u65b9\u6cd5\u5185\u53ef\u4ee5\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u62ff\u5230\u7684\u6570\u636e\u4fdd\u5b58\u5728\u5bf9\u5e94\u7684<code>entry<\/code>\u4e2d\uff0c\u8c03\u7528<code>completion<\/code>\u4e4b\u540e\u4f1a\u5230\u5237\u65b0\u5c0f\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct Provider: TimelineProvider {\n    func placeholder(in context: Context) -> SimpleEntry {\n        SimpleEntry(date: Date())\n    }\n\n    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {\n        let entry = SimpleEntry(date: Date())\n        completion(entry)\n    }\n\n    func getTimeline(in context: Context, completion: @escaping (Timeline&lt;Entry>) -> ()) {\n        var entries: [SimpleEntry] = []\n        let currentDate = Date()\n        for hourOffset in 0 ..&lt; 5 {\n            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!\n            let entry = SimpleEntry(date: entryDate)\n            entries.append(entry)\n        }\n\n        let timeline = Timeline(entries: entries, policy: .atEnd)\n        completion(timeline)\n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">SimpleEntry<\/h5>\n\n\n\n<p>\u5b9e\u73b0<code>TimelineEntry<\/code>\u534f\u8bae\uff0c\u5c31\u662f\u7528\u6765\u4fdd\u5b58\u6240\u9700\u8981\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct SimpleEntry: TimelineEntry {\n    let date: Date\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">TestWidgetEntryView<\/h5>\n\n\n\n<p>\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5c31\u662f\u6211\u4eec\u9700\u8981\u7528\u6765\u5c55\u793a\u7684\u89c6\u56fe<code>View<\/code>\uff0c\u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u754c\u9762\u642d\u5efa<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct TestWidgetEntryView : View {\n    var entry: Provider.Entry\n    var body: some View {\n        Text(entry.date, style: .time)\n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">@main struct TestWidget: Widget<\/h5>\n\n\n\n<p><code>@main<\/code>\uff1a\u4ee3\u8868\u7740<code>Widget<\/code>\u7684\u4e3b\u5165\u53e3\uff0c\u7cfb\u7edf\u4ece\u8fd9\u91cc\u52a0\u8f7d<br><code>kind<\/code>\uff1a\u662f<code>Widget<\/code>\u7684\u552f\u4e00\u6807\u8bc6<br><code>StaticConfiguration<\/code>\uff1a\u521d\u59cb\u5316\u914d\u7f6e\u4ee3\u7801<br><code>configurationDisplayName<\/code>\uff1a\u6dfb\u52a0\u7f16\u8f91\u754c\u9762\u5c55\u793a\u7684\u6807\u9898<br><code>description<\/code>\uff1a\u6dfb\u52a0\u7f16\u8f91\u754c\u9762\u5c55\u793a\u7684\u63cf\u8ff0\u5185\u5bb9<br><code>supportedFamilies<\/code>\u8fd9\u91cc\u53ef\u4ee5\u9650\u5236\u8981\u63d0\u4f9b\u4e09\u4e2a\u6837\u5f0f\u4e2d\u7684\u54ea\u51e0\u4e2a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@main\nstruct TestWidget: Widget {\n    let kind: String = \"TestWidget\"\n\n    var body: some WidgetConfiguration {\n        StaticConfiguration(kind: kind, provider: Provider()) { entry in\n            TestWidgetEntryView(entry: entry)\n        }\n        .configurationDisplayName(\"My Widget\")\n        .description(\"This is an example widget.\")\n        \/\/supportedFamilies\u4e0d\u8bbe\u7f6e\u7684\u8bdd\u9ed8\u8ba4\u4e09\u4e2a\u6837\u5f0f\u90fd\u5b9e\u73b0\n        .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])\n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5c55\u793a\u591a\u4e2a<code>Widget<\/code><\/h5>\n\n\n\n<p>\u4e00\u4e2a<code>Widget<\/code>\u53ea\u63d0\u4f9b\u4e86\u4e09\u4e2a\u6837\u5f0f\u7684\u9009\u62e9\uff0c\u6bd4\u5982\u6211\u6709\u4e2a\u9700\u6c42\uff0c\u6211\u4e0d\u9700\u8981<code>\u4e2d<\/code>\u3001<code>\u5927<\/code>\u8fd9\u4e24\u4e2a\u6837\u5f0f\uff0c\u4f46\u662f\u6211\u9700\u8981<code>\u591a\u4e2a\u5c0f<\/code>\u7684\u6837\u5f0f\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u521b\u5efa\u591a\u4e2a<code>Widget<\/code>\u4e86\u3002<br>\u8fd9\u65f6\u5019\u5c31\u8981\u7528\u5230<code>WidgetBundle<\/code>\uff0c\u628a\u4e3b\u5165\u53e3<code>@main<\/code>\u8f6c\u79fb\u5230<code>YourWidgets<\/code>\uff0c\u5b9e\u73b0<code>WidgetBundle<\/code>\u534f\u8bae\uff0c\u8fd9\u91cc\u8fd4\u56de\u591a\u4e2a<code>Widget<\/code>\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@main\nstruct YourWidgets: WidgetBundle {\n    @WidgetBundleBuilder\n    var body: some Widget {\n        OneWidget()\n        TwoWidget()\n        ThreeWidget()\n        ......  \n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>iOS14\u5e26\u6765\u4e86\u65b0\u7684UI\u7ec4\u4ef6\uff1aWidgetKit\uff0c\u4eba\u79f0\u5c0f\u7ec4\u4ef6\uff08\u7528\u8fc7\u5b89\u5353\u7684\u90fd\u80fd\u7406\u89e3\uff09\uff0c\u8fd9\u8d27\u7684\u524d\u8eab\u5176\u5b9e\u5c31\u662fiOS1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1273"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1273"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1273\/revisions"}],"predecessor-version":[{"id":1286,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1273\/revisions\/1286"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}