How to add Firebase to Today Extension iOS

放肆的年华 提交于 2019-11-27 06:01:09

问题


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.


回答1:


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

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

import FirebaseDatabase 
import FirebaseCore


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



回答2:


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.

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




回答3:


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.



来源:https://stackoverflow.com/questions/41114967/how-to-add-firebase-to-today-extension-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!