创建 iOS 实时活动时出现错误:
\nThe operation couldn\xe2\x80\x99t be completed. (com.apple.ActivityKit.ActivityInput error 1.)\nRun Code Online (Sandbox Code Playgroud)\n 我刚刚通过 Xcode 14 RC 添加了锁屏小部件。将其发送到 TestFlight 时,主屏幕小部件不会在 iOS 15 设备上显示,但会在 iOS 16 设备上显示。主屏幕和锁定屏幕小部件使用相同的文件,但我在 WidgetFamily 上有一个开关,可以为锁定屏幕显示不同的 SwiftUI 视图,并且仍然对两个小部件使用相同的 TimelineEntry。
当尝试在 iOS 15 模拟器上运行主屏幕小部件时,我收到错误:
dyld[33054]:找不到符号:_$s9WidgetKit0A6FamilyO15accessoryCorneryA2CmFWC
引用自:/Users/me/Library/Developer/CoreSimulator/Devices/0635BCE6-3EC3-44A8-AB14-7B41TED178F6/data/Containers/Bundle/Application/7EAAAE2-737A-4D13-A142-C609A921WEDS/My App.app/PlugIns /MyAppWidgetExtension.appex/MyAppWidgetExtension
预计在:/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 15.5.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/WidgetKit.framework/WidgetKit
有人经历过这个/知道如何解决这个问题吗?即使我取出 iOS 16 代码,也会出现同样的错误。我已经搜索了几个小时试图解决这个问题,但还没有解决方案有帮助。
我尝试在 WidgetBundle 中添加 iOS 17 版本检查,但小部件扩展不断崩溃。
得到Thread 1: Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value。删除 iOS 17 检查后工作正常。
有什么方法可以在 widgetBundle 中提供版本检查吗?
struct MyWidgets: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
widget1()
widget2()
if #available(iOSApplicationExtension 16.1, *) {
LiveActivity()
}
if #available(iOSApplicationExtension 17.0, *) {
ContactdWidget()
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建新的 iOS 17 交互式小部件。为了能够添加“操作”,我想使用新的init(_:intent:)初始化程序,Button它允许我在按下按钮时执行给定的意图。
问题是,我的按钮应该只是带有微笑图像的绿色内部矩形,但系统添加了自己的带有填充的背景。有什么办法可以去除吗?
示例代码:
Button(intent: MyIntent()) {
MyLabel()
}
Run Code Online (Sandbox Code Playgroud)
看起来如何:
我有一个使用 Core Data 和 CloudKit 的应用程序。更改在设备之间同步。主要目标有后台模式功能,检查Remote notificationsiCloud 功能,服务设置为 CloudKit,并检查容器中的正确容器。
我如何在代码中对记录的更改、删除和添加做出反应?我需要WidgetCenter.shared.reloadAllTimelines()在 CloudKit 中的核心数据更改以更新 iOS 14 主屏幕小部件时调用。
我的目标是让这个工作:我在 icloud.developer.apple.com 或其他设备上更改/添加/删除记录,并WidgetCenter.shared.reloadAllTimelines()调用以在小部件中显示正确的数据。应用程序可能在后台或前台。
来自AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Register for Remote Notifications
application.registerForRemoteNotifications()
return true
}
Run Code Online (Sandbox Code Playgroud)
此外,在输出日志中注意到消息:
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:]_block_invoke(2138): <NSCloudKitMirroringDelegate: 0x281818d00> - 忽略远程更改通知,因为导出器已经赶上了这个事务:64 / 64 - <NSCloudKitMirroringDelegate: 0x281818d00>网址:file:///var/mobile/Containers/Data/Application/F83C68DA-7C36-42CC-926D-7C721C679579/Library/Application%20Support/AppTitle.sqlite)
我在getTimeline 中使用以编程方式生成的 UIImage 构建我的小部件。
所以我需要在getTimeline 中知道iOS 主题是 Light 还是 Dark。
我知道如何在带有 的 ViewController 中执行此操作traitCollection.userInterfaceStyle == .dark,但是如何在 Widget 中执行此操作?
我有一个代码:
struct ContentView: View {
let entry: LessonWidgetEntry
private static let url: URL = URL(string: "widgetUrl")!
var body: some View {
VStack {
switch entry.state {
case .none:
ProgramNotStartedView()
case .currentLesson(let lesson):
LessonView(lesson: lesson, imageName: entry.program?.imageName)
case .lessonCompleted(let lesson):
LessonCompletedView(lesson: lesson)
case .programCompleted:
ProgramCompletedView()
}
}.widgetURL(ContentView.url)
}
}
Run Code Online (Sandbox Code Playgroud)
在午夜 LessonCompletedView 应该更改为 LessonView,但我不知道如何做到这一点。关于如何从小部件更改午夜视图的任何想法?
我在 iOS 14 中型小部件中显示 3 行,如下所示:
row 1
-------
row 2
-------
row 3
-------
Run Code Online (Sandbox Code Playgroud)
我的视图结构在这里:
VStack {
ForEach(records, id: \.id) { item in
ZStack {
// some views
}
.widgetURL(URL(string: "wig://\(item.id)"))
}
Divider()
}
Run Code Online (Sandbox Code Playgroud)
似乎第一项和第二项的小部件 URL被第三项覆盖,所有深层链接都将打开第三项的内容。
为 ForEach 生成的视图添加深层链接的正确方法是什么?
我只在“应用程序分析”部分看到我的主应用程序的安装,但没有关于小部件等扩展程序的内容。我在哪里可以找到这些信息?
对于我的小部件,我希望用户能够选择他们选择的小部件背景颜色,但我似乎无法让它工作。
我正在尝试用一个ColorPicker和@AppStorage
ColorPicker("Background Color", selection: Binding(get: {
bgColor
}, set: { newValue in
backgroundColor = self.updateColorInAppStorage(color: newValue)
bgColor = newValue
}))
.frame(width: 200, height: 50)
.font(.headline)
Run Code Online (Sandbox Code Playgroud)
backgroundColor 是一个@AppStorage保存 RGB 值的变量
在我的 Widget 扩展类中,我使用这个变量在 @main 结构中设置小部件背景颜色:
@main
struct MyWidget: Widget {
@AppStorage("backgroundColor", store: UserDefaults(suiteName: "group.com.MyWidget")) var backgroundColor = ""
@State private var bgColor = Color(UIColor.systemBackground)
let kind: String = "Count"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
SubscriberCountEntryView(entry: entry) …Run Code Online (Sandbox Code Playgroud)