How to add Firebase to Today Extension iOS - 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
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:
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 and YourProject.xcworkspace
Issue pod init and it will list your existing targets one by one.
Edit the Podfile by either grouping under abstract_target or using inherit!
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 console
Recreate Podfile (basically copy-paste)
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 e.g.
TodayViewExtensions (or widgets) may only be very light codewise.

Related

Analytics not recognized

Been following the Firebase setup guide. The setup process worked, but my project doesn't recognize the Analytics type:
Analytics.logEvent // use of unresolved identifier 'Analytics'
Xcode autocomplete shows AnalyticsConfiguration, but nothing else. As a sanity check, here's what I did to set up my project:
Use cocoapods to fetch Firebase/Core
Ensure I'm using the .xcworkspace file
Added the GoogleService-Info.plist for my project
In my app delegate, import Firebase and call FirebaseApp.configure()
Realized what the problem was. My project name was "Analytics", and it somehow influenced the visibility of the framework named "Analytics".

Can't find Firebase's PhoneAuthProvider class

I'm working on adding a phone authentication login to my app, but my xcode just can't find the PhoneAuthProvider class. I have included
import Firebase
import FirebaseAuth
and the right pods installed
pod ‘Firebase/Auth’
pod ‘Firebase/Database’
pod ‘Firebase/Core’
Any idea to why it's not available for me?
Apparently it was because my Firebase pods were outdated, after updating them to the newest version I was able to use the class.
To update your Firebase pods, go to terminal and type in
pod update Firebase/Auth
I'm gonna leave it here as I've found a few similar questions without any answers.
It's a problem in creation or config project or app. You need just need go:
project-> add another app
and redo all process that you can create app.
In my case that worked.
Note: don't create another project because Firebase only provide 3 project to create.
You need to go to Project> Clean Build Folder. Build the project. This fixes my problem.
In my case I had to close whole XCode and then open it again, and everything worked fine.

Firebase Class FIRAAppEnvironmentUtil is implemented in both when using Cocoapods

I'm trying to use Firebase inside a dynamic framework (Cocoa Touch Framework). I've added Firebase to the Podfile like so:
platform :ios, '9.0'
use_frameworks!
workspace 'Test.xcworkspace'
target 'Test' do
project 'Test.xcodeproj'
end
target 'TestFramework' do
project 'TestFramework/TestFramework.xcodeproj'
# Pods for TestFramework
pod 'Firebase/Database'
pod 'Firebase/Auth'
end
And I've set a target dependency in the build phase of my Test target. All according to: https://github.com/benasher44/CocoaPodsLibExample
However when I reference my TestFramework from my Test target during app startup I get this message (for all the Firebase classes) in the console:
objc[34978]: Class FIRAAppEnvironmentUtil is implemented in both
/Users/peterwarbo/Library/Developer/Xcode/DerivedData/Test-goxvmnklbfyswhdniydzbvzudcez/Build/Products/Debug-iphonesimulator/TestFramework.framework/TestFramework
(0x1085858c8) and
/Users/peterwarbo/Library/Developer/CoreSimulator/Devices/43080C45-C9E9-4237-A255-5029CC4D5797/data/Containers/Bundle/Application/FCFDC439-8564-40A8-9B34-935FE222193E/Test.app/Test
(0x104342938). One of the two will be used. Which one is undefined.
And the app crashes. I've read that this might have something to do with that Firebase is a static library. I need to solve this so I can use my use Firebase from my different targets. Specifically what I'm trying to achieve is to be able to do Firebase operations from a keyboard extension, so both my host app and my extension should be able to to perform Firebase operations.
I've been trying to solve this for more than a week now and I haven't come up with any working solutions :(
I'm using Xcode 8.2.1, Cocoapods 1.2.0

I updated the cocoapods for Firebase and my app display error messages now

I have some code lines that doesn't work anymore since I updated the Firebase with cocoapods.
For that examples, I give the error message :
override init() {
Firebase.defaultConfig().persistenceEnabled = true
}
=> Cannot call value of non-function type 'module'<'Firebase>'
That other line:
let rootRef = Firebase(url: "https://<mysite>.firebaseio.com/")
var messageRef: Firebase!
=> Cannot call value of non-function type 'module'<'Firebase>''
messagesQuery.observeEventType(.ChildAdded) { (snapshot: FDataSnapshot!) in
=> Use of undeclared type 'FDataSnapshot'
It seems that Firebase doesn't work anymore that way...
This is how you do it in the "new" Firebase.
First
override init() {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
FIRApp.configure()
}
Second
let databaseRef = FIRDatabase.database().reference()
// To get a child use .child(childName)
Third
databaseRef.observeSingleEventOfType(.ChildAdded, withBlock: {
snapshot in
print(snapshot.value!)
})
1) Look this tutorial. This guy explanes very well how to install firebase with cocoapods:
Swift: Firebase 3 - Installing SDK using Cocoapods and Saving Users into Database
2) Otherwise you can follow this step by step guide on the official page if you want to try to put firebase again:
Add Firebase to your iOS Project
Add Firebase to your app
It's time to add Firebase to your app. To do this you'll need a Firebase project and a Firebase configuration file for your app.
Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
Click Add Firebase to your iOS app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
When prompted, enter your app's bundle ID. It's important to enter the bundle ID your app is using; this can only be set when you add an app to your Firebase project.
At the end, you'll download a GoogleService-Info.plist file. You can download this file again at any time.
If you haven't done so already, copy this into your Xcode project root.
Note: If you have multiple build variants with different bundle IDs defined, each app must be added to your project in Firebase console.
Add the SDK
If you are setting up a new project, you need to install the SDK. You may have already completed this as part of creating a Firebase project.
We recommend using CocoaPods to install the libraries. You can install Cocoapods by following the installation instructions. If you'd rather not use CocoaPods, you can integrate the SDK frameworks directly by following the instructions below.
If you are planning to download and run one of the quickstart samples the Xcode project and Podfile are already present. If you would like to integrate the Firebase libraries into one of your own projects, you will need to install the pods for the libraries that you want to use.
If you don't have an Xcode project yet, create one now.
Create a Podfile if you don't have one:
$ cd your-project directory
$ pod init
Add the pods that you want to install. You can include a Pod in your Podfile like this:
pod 'Firebase/Core'
This will add the prerequisite libraries needed to get Firebase up and running in your iOS app, along with Firebase Analytics. A list of currently available pods and subspecs is provided below. These are linked in feature specific setup guides as well.
Install the pods and open the .xcworkspace file to see the project in Xcode.
$ pod install
$ open your-project.xcworkspace
Download a GoogleService-Info.plist file from Firebase console and include it in your app.
3) If you want to upgrade your existing app from Firebase.com:
Upgrade your iOS app from Firebase.com

Integrating Parse in App extension

I’m trying to implement parse functionality in a app extension. The main app uses Parse to push and retrieve data. The problem is I can't install the parse SDK to my app extension. Is this even possible? I've been on google for about three hours and I’m starting to think not. I've tried to init another pod file and I’ve tried the the abstract_target function. No matter what I do, I can't seem to import it. Has anyone achieved this before? Im using swift 2.0 if that helps.
If there is another way to carry out the following, please let me know:
(This happens in the share popup view)
1.Grab user input text
2.Push it to the parse backend
in order to use Parse ios SDK in your app extension you are require to do the following:
enable local data store (local data store is where your data is being saved locally on your device)
Enable app groups and keychain sharing in both of your targets (your main app and your extension) this allows you to share the data and the session between your app and your extension.
When i did it. I follow this guide which explain how to integrate apple watch and app extension with parse IOS SDK.
If you are using CocoaPods in your project please make sure that you load the Parse pod also to your extension..
so let's assume that the only pod that you have in your Podfile is pod 'Parse' you Podfile should look like the following
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
def my_pods
pod 'Parse'
end
# Change the MainAppTargetName to your main app target
target 'MainAppTargetName' do
my_pods
end
# change MyExtensionTargetName to your extension target name
target 'MyExtensionTargetName' do
my_pods
end

Resources