我正在尝试使用 Widget 上下文预览一些视图,例如:
\nstruct MyTasksView_Previews: PreviewProvider {\n static var previews: some View {\n MyTasksView(\n myTasks: Fake.myTasks,\n user: Fake.user,\n error: ""\n )\n .previewContext(WidgetPreviewContext(family: .systemMedium))\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n但是,我在尝试运行预览时收到此错误。而且,我不确定为什么会发生这种情况。
\n\n\nRemoteHumanReadableError:未知的预览提供程序 \xe2\x80\x9cMyTasksView_Previews\xe2\x80\x9d\nMyApp 不包含名为 \xe2\x80\x9cMyTasksView_Previews\xe2\x80\x9d 的预览提供程序。检查您的构建设置以确保预览提供程序已编译到您的产品中。
\n
我也尝试使用简单的Text(text).previewContext(WidgetPreviewContext(family: .systemMedium)),但它也不起作用。我正在使用 Xcode beta 5。
在 WWDC 2020 的 Apple Emoji Rangers Demo App 中,我们可以看到这段代码进行预览:
\nstruct CharacterNameView_Previews: PreviewProvider {\n static var previews: some View {\n CharacterNameView(CharacterDetail.panda)\n .previewContext(WidgetPreviewContext(family: .systemSmall))\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n 我添加了具有配置意图的小部件扩展。出于记录目的,我尝试记录应用程序用户拥有的小部件数量
WidgetCenter.shared.getCurrentConfigurations { results in
guard let widgets = try? results.get() else { return }
..<log widgets.count>...
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,我得到了所有小部件中曾经使用过的所有配置意图的完整列表,而不是实际的 [WidgetInfo]。
即我屏幕上有 2 个小部件,而WidgetCenter声称我有 9 个当前配置。关于如何计算小部件有更好的想法吗?
iOS 14 Beta 8(iPhone 和模拟器)和 Xcode 12 Beta 6
现在我的 Widget 每秒发出大约 1 个请求。我想将其更改为 1 小时内 1 个请求。
我在网上遇到了一些let timeline = ...类似的错误Value of optional type 'Date?' must be unwrapped to a value of type 'Date'以及更多错误。
任何可能出错的建议:
struct Provider: IntentTimelineProvider {
let networkManager = NetworkManager()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubname: networkManager.clubName)
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration, clubname: networkManager.clubName)
completion(entry)
}
func getTimeline(for configuration: …Run Code Online (Sandbox Code Playgroud) 我们到底需要在里面指定什么 URL .widgetURL()?
在 WWDC 会议上关于 Widget Code Below 的会议中,他们使用了.widgetURL(entry.character.url). 这里的entry.character.url是URL(string: "game:///egghead")!
请解释一下我们需要通过这里(widgetURL())的 URL 才能到达 APP 的特定视图?
Apple Notes 小部件展示了理想的示例。添加 Apple Notes 小部件时,WidgetKit 会继续为新小部件分配一个 IntentConfiguration。您可以通过编辑小部件来查看这一点,并注意到选择元素不是“选择”,而是包含自动选择为新小部件默认值的笔记或文件夹的名称。您还会注意到,此占位符小部件的数据源不会从一个源更改为另一个源 - 分配的 IntentConfiguration 受到尊重。
我们如何在我们的小部件中做到这一点?它应该看起来像这样:有人创建了一个新的小部件,我们使用他们最近的数据项作为占位符,并且该项目 1) 保留在小部件中,直到他们更改它 2) 反映在编辑小部件对话框中。
如何在小部件 swiftui ios 14 中加载 wkwebview 我正在做这样的事情(附加代码)但它向我展示了这张图片https://www.sendspace.com/file/nw002i(从这个链接下载图片)期待您的顺便说一下,我是 swiftui 的新手,我很好奇苹果是否允许这样做?
import WidgetKit
import SwiftUI
import WebKit
import Photos
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: …Run Code Online (Sandbox Code Playgroud) 该应用程序使用 Core Data + CloudKit。当应用程序启动时,WidgetKit 会从核心数据中获取正确的条目,但是当我从主应用程序向核心数据添加新条目并调用WidgetCenter.shared.reloadTimelines()更新的条目时,不会获取到旧数据,并且旧数据会显示在小部件内。
主应用程序和小部件目标具有相同的应用程序组,两个目标都启用了 iCloud 功能。
当我WidgetCenter.shared.reloadTimelines()从主应用程序调用小部件重新加载(添加到小部件视图的计时器设置为零)但从核心数据获取时,不会返回新添加的条目。然后我重新启动应用程序并获取正确的条目。
为什么当 WidgetCenter.shared.reloadTimelines() 被调用时它不获取正确的条目并且仅在应用程序重新启动时才起作用?
小部件的代码:
import WidgetKit
import SwiftUI
import CoreData
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> HabitsEntry {
HabitsEntry(date: Date(), habitsCount: 0)
}
func getSnapshot(in context: Context, completion: @escaping (HabitsEntry) -> ()) {
let entry = HabitsEntry(date: Date(), habitsCount: 0)
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let managedObjectContext = Storage.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "DailyHabit")
let predicate …Run Code Online (Sandbox Code Playgroud) 我有我的图像 Assets.xcassets,并尝试将其加载到我的小部件视图中。为了也在小部件中使用图像,我已经为小部件扩展设置了目标成员资格。但问题是它无法加载某些图像(例如HalloweenOne,,,)。每个图像的大小都在 10kb 以下。(所有图像都在应用程序中加载良好。)HalloweenTwoHalloweenThree
Image("HalloweenOne")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 50)
Run Code Online (Sandbox Code Playgroud)
我想在我的主应用程序中预览我的 WidgetKit 小部件。
小部件的布局.systemSmall和.systemMedium大小不同。
当我.environment(\.widgetFamily, .systemSmall)在主应用程序中设置时,出现构建错误:
Key path value type 'WritableKeyPath<EnvironmentValues, WidgetFamily>' cannot be converted to contextual type 'KeyPath<EnvironmentValues, WidgetFamily>'
Run Code Online (Sandbox Code Playgroud)
如果我不设置值,则默认为.systemMedium.
有办法使用吗.systemSmall?
我想在我的应用程序中添加一个超大的小部件作为 iOS 15 支持的系列。
WidgetConfiguration的简化代码如下:
var body: some WidgetConfiguration {
IntentConfiguration(
kind: "Widget",
intent: SelectProjectIntent.self,
provider: Provider()
) {
entry in
ProgressWidgetEntryView(entry: entry)
}
.configurationDisplayName("Title")
.description("Description")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
}
Run Code Online (Sandbox Code Playgroud)
显然我不能只是简单地添加额外的大,因为出现以下错误: “systemExtraLarge”仅在 iOS 15.0 或更高版本的应用程序扩展中可用
但是,按照 XCode 的建议进行快速简单的可用性检查后,我收到了一个错误和几个警告。这是代码:
var body: some WidgetConfiguration {
if #available(iOSApplicationExtension 15.0, *) {
IntentConfiguration(
kind: "Widget",
intent: SelectProjectIntent.self,
provider: Provider()
) {
entry in
ProgressWidgetEntryView(entry: entry)
}
.configurationDisplayName("Title")
.description("Description")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
} else {
IntentConfiguration(
kind: "Widget",
intent: SelectProjectIntent.self,
provider: Provider()
) …Run Code Online (Sandbox Code Playgroud)