问题
I'm building an iOS app for iOS 10+. The app builds fine in Debug configuration, but in Release fails to compile swift source code that references WidgetCenter
.
It outputs the error Cannot find WidgetCenter in scope
even though I'm importing WidgetKit
and optionally embedding the framework.
import Foundation
import WidgetKit
class MyWidgetCenter: NSObject {
@available(iOS 14, *)
func reloadTimelines(_ kind: String) {
// this line causes error: cannot find 'WidgetCenter' in scope
WidgetCenter.shared.reloadTimelines(ofKind: kind)
}
@available(iOS 14, *)
func reloadAllTimelines() {
// this line causes error: cannot find 'WidgetCenter' in scope
WidgetCenter.shared.reloadAllTimelines()
}
}
Edit:
It builds fine for the simulator and my connected device (iPhone XR) in Release configuration when I set the Build Active Architecture Only
flag. It's only when it's building for multiple architectures that it fails to compile. Are there architecture restrictions for WidgetKit
that I'm not accounting for?
回答1:
I believe WidgetKit is supposed to support the armv7
architecture, however it fails to compile usage of WidgetCenter
for armv7
.
My workaround is to wrap the WidgetCenter
statements, to only support the architectures I need outside of armv7
class MyWidgetCenter: NSObject {
@available(iOS 14, *)
func reloadTimelines(_ kind: String) {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadTimelines(ofKind: kind)
#endif
}
@available(iOS 14, *)
func reloadAllTimelines() {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadAllTimelines()
#endif
}
}
回答2:
You should first import WidgetKit
in your file, and add the function reloadAllWidget
below.
@objc public func reloadAllWidget() {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadAllTimelines()
#endif
}
When you want to update timeline, you use self.reloadAllWidget()
回答3:
In my case, I have to check only for arm64
.
@available(iOS 14, *)
@objc public func reloadWidget() {
// Compiler error fix. Arm64 - current 64-bit ARM CPU architecture,
// as used since the iPhone 5S and later (6, 6S, SE and 7),
// the iPad Air, Air 2 and Pro, with the A7 and later chips.
#if arch(arm64)
WidgetCenter.shared.reloadAllTimelines()
#endif
}
来源:https://stackoverflow.com/questions/63065117/widgetcenter-not-found-in-scope