8

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?

Taylor Johnson
  • 1,845
  • 1
  • 18
  • 31

3 Answers3

21

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
  }
}
Taylor Johnson
  • 1,845
  • 1
  • 18
  • 31
3

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()

BackSpace
  • 119
  • 1
  • 4
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
}
Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34