Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Firebase to Today Extension iOS

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.

like image 681
Eli Avatar asked Dec 13 '16 06:12

Eli


1 Answers

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

like image 171
toraritte Avatar answered Oct 04 '22 01:10

toraritte