iOS 14 Widgets + SwiftUI + Firebase?

后端 未结 3 1303
旧巷少年郎
旧巷少年郎 2020-12-31 21:17

I\'m still pretty new to SwiftUI and Firebase. Recently, as a hobby, I have been developing an app for my school. After the launch of Xcode 12, I decided to experiment with

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 22:09

    I can confirm after testing that the following method works to use Firebase in the Widget Target without incorporating an app group, user defaults or anything else.

    @main
    struct FirebaseStartupSequence: Widget {
      init() {
        FirebaseApp.configure()
      }
    
      let kind: String = "FirebaseStartupSequence"
    
      var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
          FirebaseStartupSequenceEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
      }
    }
    

    Simply use the init method in your widget to access a firebase instance.

    This was the easiest solution for me as of today.

    Taken from: https://github.com/firebase/firebase-ios-sdk/issues/6683

    Additional Edit: Do you need to share authentication? No problem. Firebase has that covered here: https://firebase.google.com/docs/auth/ios/single-sign-on?authuser=1

提交回复
热议问题