问题
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:
- Copy the containing app's
GoogleService-Info.plist
into your extension in Xcode - Drag the copied
GoogleService-Info.plist
into Xcode into your share extension and - Change the BUNDLE_ID to the name of your share extension's target
- Add new target to your
Podfile
- Install dependencies (
pod install
) - 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?
Remove
Podfile
,Podfile.lock
andYourProject.xcworkspace
Issue
pod init
and it will list your existing targets one by one.Edit the
Podfile
by either grouping underabstract_target
or usinginherit!
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:
wiped everything clean by cloning our project repo fresh from Github,
deleted
- ~/Library/Developer/Xcode/DerivedData
- ./Pods/
- Podfile
- Podfile.lock
Issue
pod init
on the consoleRecreate
Podfile
(basically copy-paste)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