Cannot import FirebaseMessaging in Service Extension target - ios

I have a SwiftUI project where I added the Firebase dependency using the "Add Package" at the project level.
I could then put 'import FirebaseMessaging' inside a swift file and it would build without error, so I know the dependency is added correctly.
I then added a UNNotificationServiceExtension by going to File > New > Target and adding a Notification Service Extension.
Now in code in my new target in the same project, 'import FirebaseMessaging' will not resolve.
If I right click on the service extension and select 'Add Package', it will not allow me to add the firebase sdk package to the service extension, as it is already added to the project.
How can I get the Firebase dependency to be added to my service extension target?

I think I figured out a solution! Although the package has been added to the project, it needs to be explicitly added to the Service Extension as well.
Once it's been added, you should be able to import FirebaseMessaging from the Service Extension.

Related

Is there a way to add Flutter to an extension Target on iOS xCode?

Assuming I have a basic flutter app and I add a target in xcode for the ios app. Would there be any way to link it to the main Flutter folder so I can do an "import Flutter" in the extension the same way it's done in the AppDelegate then start a FlutterEngine, MethodChannel etc locally in the extension ?
I partially succeded adding a module compiled as an "ios-framework" (otherwise with cocoapods there's a dependency conflict error) to the extension xcode target, yet this means including a redundant Flutter dependency and although it runs, I'm not sure at this point this is very stable... For this reason I'm wondering if there's a way to make an import of the main Flutter directly ?
Thanks

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.

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

Import React Native dependency to existing iOS app manually

It seems like just about every tutorial and blog post out there has you integrating RN into an existing app using CocoaPods. Adding CocoaPods support isn't a possibility for our existing iOS app - we are relying on Carthage for dependency management already which doesn't seem to be supported by RN.
So, I went through the process of adding the dependencies manually by attempting to recreate the iOS app that is produced when using the RN CLI (react-native init):
-Add the React.xcodeproj from node_modules/react-native to my Libraries folder, and also add the other Libraries from node_modules/react-native/Libraries, such as RCTActionSheet.xcodeproj. Then, I added all of those to the 'Link Binary With Libraries' section in Build Phases.
-Updated 'Other Linker Flags' to -ObjC -Ic++.
...My app build is still failing with "RCTRootView.h file not found".
Does anyone have experience with just adding RN manually to an existing iOS project, that might be able to provide a definitive list of steps for adding it manually to an existing project?
In addition to the other steps, I also needed to update the HeaderSearchPaths setting to point to the React folder in node_modules: http://i.imgur.com/jK0IMZN.jpg

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.

Resources