I am making some API Firebase calls (download some user specific data) in my app share extension but the user is not signed into Firebase (aka currentUser
is nil) so I don't get any data because I need the uid. In other words, the database only allows authenticated users to access the data. How can I fix this?
To expand on KutakMir's answer, these steps worked for us:
Steps from this Stackoverflow answer:
pod install
)Described in App Extension Programming Guide: Handling Common Scenarios. Basically turn on "App Groups" at the Capabilities tab for both your targets (i.e., containing app and share extension), and provide the same designation at both places.
The basic steps are similar to step 2: turn on "Keychain Sharing" at the Capabilities tab for both targets, and provide the designation at both places. (In more detail in the official docs.)
Make sure to append your APP_ID to the Keychain access group when using the queries in the docs! The link above points this out, but this Github issue comment is more useful.
Our workflow:
On login (either in share extension or main app), save user ID to the shared container, and save username and password to the Keychain using the user ID as the key
On startup (either share extension or main app), check shared container for user ID. If there, it means that user already signed in either the main app or the share extension therefore they should be signed into both. So retrieve credentials from Keychain, and sign in user using signIn(withEmail:password:completion:)
Our project at the point when everything was set up. It is messy with lots of duplication, but works. Relevant files are
This was the most frustrating part. The error codes are of type OSStatus
, and they are listed in Security Framework Result Codes, but sometimes one just sees a signed integer when testing. I couldn't find an official list, but OSStatus.com will save a lot of frustration. For example, here's a query of all the Security Framework codes.
Other useful links to official Keychain docs:
kSecValueData
(it wasn't where I expected it to be)Go to your main target and extension target and setup keychain sharing. The Firebase Auth is saving the access token to the keychain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With