Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WidgetCenter not found in scope

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?

like image 861
Taylor Johnson Avatar asked Jul 24 '20 00:07

Taylor Johnson


Video Answer


1 Answers

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
  }
}
like image 132
Taylor Johnson Avatar answered Oct 04 '22 19:10

Taylor Johnson