12

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

Thanks.

AL.
  • 36,815
  • 10
  • 142
  • 281
Eli
  • 668
  • 2
  • 13
  • 37

3 Answers3

16

You have to treat the today extension as its own separate app(somewhat)

in your firebase project dashboard, you need to hit the "Add another app" 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 your Today Extension's root folder

go to your xcode project, and manually add FirebaseCore.framework and FirebaseDatabase.framework to your Today Extension

step 1 : get your bundle id step 2 : add frameworks

finally inside your today today viewcontroller call FirebaseApp.configure()

import FirebaseDatabase 
import FirebaseCore


override func viewDidLoad() {
    super.viewDidLoad()
    FirebaseApp.configure() 
}
CharlesAE
  • 907
  • 12
  • 16
  • Seen as it is two different 'apps' does that mean that I cannot use data from where all my data is? Eg can my extension 'app' access the data in my current database? – Eli Jul 15 '17 at 11:38
  • 2
    ok just to clear a few things up... 1) At your Firebase Console (https://console.firebase.google.com/) 2) Choose your EXISTING Firebase Project. 3) You are then adding an "app" to your EXISTING Firebase Project. 4) Your Firebase Database(for your EXISTING Firebase Project) will be accessible to all "apps" within that EXISTING Firebase Project. 5) Please select my answer if this solution has worked for you :-) – CharlesAE Jul 18 '17 at 17:53
  • Thanks for the answer, but can we add the Firebase not manually but via pod to the app and today extension? I've tried to do that but my app cruch! please check my question here: https://stackoverflow.com/q/46826315/4498576 I will be sooo thankful for any help :( – Rawan Oct 23 '17 at 09:37
  • 2
    it seems it's required to check that [FIRApp defaultApp] is not null already to not to have a crash that the app is already configured – Alexey Mar 27 '18 at 13:42
  • But what if the user needs to be logged in? – Supertecnoboff Oct 12 '18 at 20:23
  • you can share data (im guessing a session token or something) between the widget and the main app by enabling "app groups" in both of their respective target capabilities. – CharlesAE Oct 14 '18 at 01:47
  • Your solution looks correct but there is an issue that I'm facing right now. I perfectly implement Firebase and configure FirebaseApp but I couldn't observe data. Do you have any idea why? @CharlieNorris – Esat kemal Ekren Nov 14 '19 at 15:05
11

Or without adding an extra app at the Firebase console, just re-use your main project's GoogleService-Info.plist with a minor modification (see below). The Firebase app singleton would have to be configured either way in both apps on startup.

To synchronize an extension and the containing app see App Extension Programming Guide: Handling Common Scenarios or this reddit comment. This Stackoverflow thread specifically describes this scenario.

Steps:

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

Step 1. Copy the containing app's GoogleService-Info.plist into your extension in Xcode

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

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

For us the main (i.e., containing app) is Access News and the share extension is Access-News-Uploader.

enter image description here

enter image description here

Step 4. Add new target to your Podfile

# ...

target 'name-of-your-extension' do

  use_frameworks!

  pod 'Firebase/Core'
  pod 'Firebase/Auth'
  # etc.
end

Entire Podfile of our project.

Step 5. Install 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()
        }
        /*************************/

        // ...
    }

Pod issue fixes

1) Still unable to import Firebase!

Make sure that pods are installed for all targets in your project. To achieve this use inherit! or abstract_target in your Podfile.

The simplest example 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 do I achieve this on my existing app without messing things up?

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

  2. Issue pod init and it will list your existing targets one by one.

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

  4. Issue pod install

A new YourProject.xcworkspace file will be generated, and if you open the your project using this, under General > Linked Frameworks and Libraries it will show that Firebase is added and can be imported from project files.

(See this SO thread for a concrete example where this clean-up method needed to be used.)

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

This is what worked for me:

  1. wiped everything clean by cloning our project repo fresh from Github,

  2. deleted

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

  4. Recreate Podfile (basically copy-paste)

  5. Issue pod update on the console

(Probably won't work next time.)

toraritte
  • 6,300
  • 3
  • 46
  • 67
  • i am already done to istall firebase but issue is Firebase Database is not updating in i am already done to istall firebase but issue is Firebase Database is not updating in UNNotificationServiceExtension what should i do? what should i do? – Shakeel Ahmed Jul 25 '19 at 11:58
  • he project's Bundle ID is inconsistent with either the Bundle ID in 'GoogleService-Info.plist', or the Bundle ID in the options if you are using a customized options. To ensure that everything can be configured correctly, you may need to make the Bundle IDs consistent. To continue with this plist file, you may change your app's bundle identifier to bundle id. Or you can download a new configuration file that matches your bundle identifier from https://console.firebase.google.com/ and replace the current one. – Shakeel Ahmed Jul 26 '19 at 07:03
  • Thank you! I haven't been doing iOS dev lately, and don't even have the hardware anymore to test it, so please feel free to update the answer if there are any inaccuracies. – toraritte Jul 26 '19 at 15:50
  • one other issue i am fail to get the permisson of firebase auth this is an issue – Shakeel Ahmed Jul 29 '19 at 09:27
-4

To my knowledge widgets are not allowed to use certain api's such as firebase. Widgets are supposed to show data provided by the main application via UserDefaults e.g.

TodayViewExtensions (or widgets) may only be very light codewise.

Fabbou
  • 28
  • 5
  • 2
    I don't believe so, all I want to do is to show the most recent post that someone made to the thread. Iv seen lots of other apps do this, for example Flipboard – Eli Dec 15 '16 at 02:01