我们有“今日小部件”,可以在很长一段时间内完美运行。引入新的小部件扩展后,我们在应用程序中添加了小部件捆绑包。现在,每次应用程序更新后,旧的小部件都会从“今日视图”中消失,并且只能通过重新启动 iPhone 才能恢复。
有时,当它们消失时,今天的视图中会出现小部件包中的第一个小部件。我也测试了其他应用程序,每次在支持新旧小部件的应用程序(例如小米家庭应用程序)上都会发生这种情况。
有人知道如何解决这个问题吗?
我想直接从应用程序添加一个小部件到主屏幕。
现在,用户可以从主屏幕转到小部件菜单,搜索应用程序小部件并添加他喜欢的小部件,但这比应有的要复杂得多,因为应该提示用户从应用程序内部创建它。
我想在 iOS 小部件中显示ble 服务特征值,就像我已经成功开发了血压应用程序来显示收缩压和舒张压值一样。那么,该应用程序一定没有在后台运行。仅小部件按特定时间更新并显示其中的最新值。如果有人在小部件中实现了 ble 的任何通信,请指导。指导一下怎么做?我搜索了很多,只找到了问题,没有找到答案,请分享您的发现。提前致谢。即使有人只是在小部件中显示蓝牙打开或关闭,请在小部件中分享或任何 ble 通信。
我正在使用 XCode 14 beta,由于某种原因,我的小部件无法在 iOS 15 中运行,而只能在 16 中运行。我还收到此错误消息:
SendProcessControlEvent:toPid: encountered an error: Error Domain=com.apple.dt.deviceprocesscontrolservice Code=8 "Failed to show Widget 'com.click.distanceTester2.Lockscreen-Widgets' error: Error Domain=FBSOpenApplicationServiceErrorDomain Code=5 "The request to open "com.apple.springboard" failed." UserInfo={NSLocalizedDescription=The request to open "com.apple.springboard" failed., NSLocalizedFailureReason=Unexpected error type., NSUnderlyingError=0x60000238c660 {Error Domain=BSServiceConnectionErrorDomain Code=3 "XPC error received on message reply handler" UserInfo={BSErrorCodeDescription=OperationFailed, NSLocalizedFailureReason=XPC error received on message reply handler}}, BSErrorCodeDescription=InvalidResponse}." UserInfo={NSLocalizedDescription=Failed to show Widget 'com.click.distanceTester2.Lockscreen-Widgets' error: Error Domain=FBSOpenApplicationServiceErrorDomain Code=5 "The request to open "com.apple.springboard" failed." UserInfo={NSLocalizedDescription=The request to open "com.apple.springboard" …Run Code Online (Sandbox Code Playgroud) 我正在使用 SwiftUI 创建 iOS 14+ Widget,我面临着非常奇怪的情况。问题是我的小部件中只有一个“完整小部件”图像(也许值得注意的是,它是从互联网下载的,但在显示其已加载并注入时UIImage),该图像正确显示如下:
但过了一段时间,更多的 iPhone 解锁 BOOM 后,我得到了这个:
一些观察:
systemLarge- 图像宽度的相同百分比被裁剪为了方便起见,我创建了导致错误的最小示例:
所以,我有这个EntryView
struct EntryView: View {
var viewModel: EntryViewModel
var body: some View {
Image(uiImage: image)
.resizable()
.scaledToFill()
}
}
Run Code Online (Sandbox Code Playgroud)
用作Widget其自身的内容
struct SomeWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(
kind: xxx,
provider: SomeWidgetTimelineProvider(xxx)
) { item in
view(for: item)
}
.configurationDisplayName(xxx)
.description(xxx)
.supportedFamilies([.systemMedium, .systemLarge])
}
private func view(for item: Item) -> some View { …Run Code Online (Sandbox Code Playgroud) 我制作了一个非常基本的 iOS Widget 扩展(在 iOS 16 上):
struct TotoView: View
var body: some View {
VStack {
Text("Toto")
.font(.headline)
}
}
}
struct TotoWidget: Widget {
var body: some WidgetConfiguration {
IntentConfiguration(kind: "myKind", intent: ViewTodayIntent.self, provider: TimelineProvider()) { entry in
TotoView(entry: entry)
}
.configurationDisplayName("Today work")
.description("Show today work sessions")
.supportedFamilies([.systemSmall, .systemMedium])
}
}
Run Code Online (Sandbox Code Playgroud)
在 SwiftUI Canvas 预览中,我可以看到“Toto”文本。
但是当我选择小部件目标并在模拟器中运行时,我只有占位符代替文本。知道为什么吗?请注意,不仅文本被占位符替换,而且图像(systemName:)
我认为这个问题似乎与 IntentConfiguration 有关(因为 StaticConfiguration 一切正常)
我使用的是 xCode 14。
我有一个工作时钟小部件。为了实现这一目标,我创建了一个每天更新一次的时间表。
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let midnight = Calendar.current.startOfDay(for: Date())
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
let entries = [SimpleEntry(date: midnight)]
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
Run Code Online (Sandbox Code Playgroud)
小部件显示时钟如下:
var body: some View {
VStack {
Text(entry.date, style: .timer)
}
}
Run Code Online (Sandbox Code Playgroud)
我选择了这种方法,而不是每分钟创建一个条目时间线,因为我在文档中读到,系统不承诺按要求更新小部件,而是根据预算。
标签显示如下:
14:30:25
我怎样才能让它显示
改为下午 2:30
在小部件内部,有一个按钮,
Button(intent: AnAppIntent()) {
// Button's label.
}
// It seems this modifier does not add any value.
.invalidatableContent()
Run Code Online (Sandbox Code Playgroud)
连接到 AppIntent。
struct AnAppIntent: AppIntent {
static var title: LocalizedStringResource = "An AppIntent"
init() {
// AppIntent required init.
}
func perform() async throws -> some IntentResult {
// Never called when the app is running.
return .result()
}
}
Run Code Online (Sandbox Code Playgroud)
该按钮在点击时会调用 AppIntent ,因此只有在应用程序完全关闭时perform()才会更新小部件 UI(无论是否带有修饰符.invalidatableContent())。
如果应用程序在后台运行,perform()则不会被调用,并且小部件 UI 永远不会更新。
用户必须明确关闭应用程序才能使小部件按预期工作。
问题可能出在所使用的时间线上。
struct SimpleEntry: TimelineEntry {
let …Run Code Online (Sandbox Code Playgroud) 我的应用程序及其小部件之间共享一个应用程序组。
我有 SwiftData 的 @Model,从小部件意图的perform()方法更新并显示在应用程序和小部件上。
@MainActor
func perform() async throws -> some IntentResult {
let context = try ModelContainer(for: MyModel.self).mainContext
let myModel = try ModelContainer(for: MyModel.self).mainContext.fetch(
FetchDescriptor<MyModel>(predicate: #Predicate {
// The predicate is not the problem.
})
).first
myModel?.functionThatModifiesMyModel()
return .result()
}
Run Code Online (Sandbox Code Playgroud)
应用程序和小部件 SwiftUI 都使用宏查看对模型的访问。
@Environment(\.modelContext) var context
@Query(sort: \.whatever) var myModel: [MyModel]
Run Code Online (Sandbox Code Playgroud)
但应用程序代码中的数据永远不正确(使用意图正确更新小部件的数据)。
为什么模型没有出现在应用程序中?
更新到 Xcode beta 并构建/运行我的应用程序后,我开始在所有小部件上收到错误“请采用 containerBackground API”,并且我找到了一篇文章,其中提供了适应 iOS 14-16 的解决方案
import Foundation
import SwiftUI
extension View {
@ViewBuilder
func widgetBackground() -> some View {
let gradient = LinearGradient(gradient: Gradient(colors: [Color("LightBlue"), Color("DarkBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
if #available(watchOS 10.0, iOSApplicationExtension 17.0, iOS 17.0, macOSApplicationExtension 14.0, *) {
self.containerBackground(gradient, for: .widget)
} else {
self.background(gradient)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将其添加到我的 ZStack 中作为小部件,但它什么也没做。采用containerBackground API 时我仍然遇到相同的错误。我究竟做错了什么?