简体   繁体   中英

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.

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.

在此输入图像描述

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

在此输入图像描述

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

第1步:获取您的bundle id第2步:添加框架

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

import FirebaseDatabase 
import FirebaseCore


override func viewDidLoad() {
    super.viewDidLoad()
    FirebaseApp.configure() 
}

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 (ie, containing app) is Access News and the share extension is Access-News-Uploader .

在此输入图像描述

在此输入图像描述

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

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 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM