Analytics not recognized - ios

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

Related

No such module 'Firebase' - Xcode and Swift Package Manager

I'm trying to incorporate Firebase Analytics into my SwiftUI project in Xcode. I've added the Firebase package using Swift Package Manager, and am able to call FirebaseApp.configure() to initialize my app in my UIApplicationDelegate class. Now I'm trying to log analytics events and am running into an issue.
My UIApplicationDelegate class is in an iOS-specific folder. I have a service class that is in a Shared folder (to be used across both iOS and macOS builds). In my service class, I've added a line that says:
import Firebase
However, when I go to build my iOS target I get an error saying:
No such module 'Firebase'
I don't know why this import statement would cause a problem, since I have the very same statement in my UIApplicationDelegate class. The only thing I could think of was that somehow my Shared classes don't know about Firebase? Maybe? When I view the iOS target in my project it shows that the FirebaseCrashlytics and FirebaseAnalytics frameworks have been added to it.
I'm at a loss as to what's happening. All other things I've found online are for Cocoapods, which I'm not using for dependency management. I'm leveraging the Swift Package Manager for this. Any help would be greatly appreciated!!
I had the same issue but the post below from GitHub should fix it:
https://github.com/firebase/firebase-ios-sdk/issues/9014#issuecomment-979489434
After installing the Firebase swift package, make sure you add the Firebase libraries to Frameworks:
TARGETS -> Your App -> General -> Frameworks, Libraries, and Embedded Content -> hit '+' to add the Firebase libraries you want:
I had the same error but with 'FirebaseCore'. I tried everything, but what worked for me was to add #_implementationOnly to import. So it should look like this:
#_implementationOnly import FirebaseCore
Here is the link that helped me and it says:
You could use #_implementationOnly import for the modules from the
third-party SDK if you aren't using anything from that SDK as part of
your module's public interface.

Crash on FIRApp.configure() method

I added a firebase framework using cocoapods to existing Project but a crash occurs at the FIRApp.configure() method.
To figure out the problem, I create a new project and add a firebase Framework which is working.
I don't know what could be the Problem.
Make sure GoogleService-Info.plist is in your build target.

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

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

Could not configure Firebase InstanceID

hi I'm trying to include firebase in my app and followed the documentation, when I'm trying to do google sign in, Could not configure Firebase InstanceID error occurs .please advice how to overcome this error
There are two things to check (and hopefully, one will fix it for you):
Make sure your BundleIdentifier is exactly the same in your Firebase project as it is in your Xcode, i.e. com.[yourcompany].[yourappname]
Make sure you got the latest GoogleService-Info.plist file. Go to Firebase Console -> Settings -> Your Application - there will be a blue button to the right that lets you download new configuration file
That solved it for me.
Bug Details
As of version 3.13.0, there appears to be a bug in the Firebase/Core SDK.
I added a symbolic breakpoint at -[FIRInstanceID(FIRApp) configureInstanceIDWithOptions:app:] and stepped though the assembly.
It appears that FIRApp.configure() is ignoring the IS_GCM_ENABLED flag in the GoogleServices-Info.plist and tries to configure GCM regardless of the flag's state. In my case, because my project does not use GCM, the app would crash because there was no GCM_SENDER_ID in the plist.
Workaround
Add a dummy value (i.e. 123456789012) for GCM_SENDER_ID within the GoogleService-Info.plist file. This will allow the application to configure successfully.
You have to add your project which is created on
https://developers.google.com/mobile/add?platform=ios or fire base console into your Firebase dashboard.
Now you can download GoogleService-Info.plist from Firebase and import into your project.
I setup Firebase in code using FIROptions. In my case, I was missing gcmSenderID, fixing it then it works
Firebase DevRel here. This should be an error, unless you enabled gcm, you shouldn't go through instanceid setup. Filed internally.
For now, the workaround is manually changing "IS_GCM_ENABLED" to "NO" in your "GoogleService-Info.plist" file
I was getting this error as I didn't have my GoogleService-Info.plist in my Test folder as well. ( if you are using TDD)
As of the latest Firebase Core 3.16.0 - I seem to need to include it in my main project as well as copying a version into my Test Root.
This also happens when you shift your google SDK integration from one account to another and don't edit the API keys on application side.Make sure when you shift from one google account to another, you re-visit all the SDK guides again like FCM, Google Sign-in etc and make sure you are using new API keys (client_id, URI Scheme) in application.

Resources