How to add Firebase to Today Extension iOS

I need to be able to use Firebase in my Today View, but I cannot import the Firebase module. I think because I need a new target in my cocoa pods file, but I'm not sure how to do this.

Thanks.

+7
source share
3 answers

You should consider today's extension as your separate application (several)

in the toolbar of the firebase project, you need to click the "Add another application" button.

enter image description here

select iOS and then enter the BUNDLE ID of your TODAY EXTENSION

enter image description here

Complete the wizard and download the generated GoogleService-Info.plist file

Add the plist file to the Today root folder >

go to the xcode project and manually add FirebaseCore.framework and FirebaseDatabase.framework to your extension today

step 1: get your package id 2: add frameworks

finally inside your today's call to viewcontroller FirebaseApp.configure ()

import FirebaseDatabase import FirebaseCore override func viewDidLoad() { super.viewDidLoad() FirebaseApp.configure() } 
+10
source

Or, without adding an additional application to the Firebase console, just reuse your main GoogleService-Info.plist project with minor changes (see below). A singleton Firebase application must be configured in both cases at startup.

To synchronize the extension and the containing application, see Application Extension Programming Guide: Handling Common Scripts or this Reddit comment . fooobar.com/questions/2433675 / ... specifically describes this scenario.

Steps :

  1. Copy the containing GoogleService-Info.plist application to your extension in Xcode
  2. Drag the copied GoogleService-Info.plist into Xcode into the extension of your GoogleService-Info.plist resource and
  3. Change BUNDLE_ID to the name of your share extension target.
  4. Add a new target to your Podfile
  5. Install dependencies ( pod install )
  6. Configure the Firebase application object in your extension

Step 1. Copy the containing GoogleService-Info.plist application to your extension in Xcode

Step 2. Drag the copied GoogleService-Info.plist into Xcode into the extension of your GoogleService-Info.plist resource and

Step 3. Change BUNDLE_ID to the name of your share extension target.

For us, the main (that is, containing the application) is Access News and the Access-News-Uploader extension is Access-News-Uploader .

enter image description here

enter image description here

Step 4. Add a new target to your Podfile

 # ... target 'name-of-your-extension' do use_frameworks! pod 'Firebase/Core' pod 'Firebase/Auth' # etc. end 

The whole Podfile our project .

Step 5. Install the dependencies ( pod install )

Step 6. Configure the Firebase application object in your extension

 /* 1. Import Firebase */ /**********************/ import Firebase /**********************/ class WhereverInYourExtension: WhateverController { // ... override func viewDidLoad() { super.viewDidLoad() /* 2. Configure Firebase */ /*************************/ if FirebaseApp.app() == nil { FirebaseApp.configure() } /*************************/ // ... } 

Bugs fixed

1) Still can not import Firebase!

Make sure the modules are installed for all purposes in your project. To achieve this, use inherit! or abstract_target in your Podfile.

The simplest example of using abstract_target from the official documentation :

 abstract_target 'Networking' do pod 'AlamoFire' target 'Networking App 1' target 'Networking App 2' end 

For inherit! See this SO question and answer .

2) How can I achieve this in my existing application without getting confused?

  1. Delete Podfile , Podfile.lock and YourProject.xcworkspace

  2. Run pod init and it will list your existing goals one by one.

  3. Edit the Podfile by grouping under abstract_target or using inherit!

  4. pod install release

There will be YourProject.xcworkspace new YourProject.xcworkspace file, and if you open your project using this, in the " General >" section of Linked Frameworks and Libraries it will show that Firebase has been added and can be imported from project files.

(See this SO thread for a specific example where this cleanup method should have been used.)

3) firebase 'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.

Here is what worked for me:

  1. wiped everything clean by cloning the repo of our project with Github,

  2. remote

    • ~ / Library / Developer / Xcode / DerivedData
    • . / Pods /
    • Podfile
    • Podfile.lock
  3. Issue pod init on the console

  4. Recreate Podfile (mostly copy-paste)

  5. Release pod update on console

(Probably will not work next time.)

+2
source

As far as I know, widgets are forbidden to use certain api, such as firebase. Widgets should show data provided by the main application through UserDefaults , for example.

TodayViewExtensions (or widgets) can be very easy.

0
source

Source: https://habr.com/ru/post/1013116/


All Articles