问题
We're building a today widget on top of our iOS app which is working with Firebase. However we're struggling to access current signed-in user from the extension. The user at the main app is anonymous so passing credentials via shared container is not an option.
The only way that I found is passing uid and generate custom token to sign in but I was hoping to find out better way to share FIRUser between my main app and app extension. What would be the best way to achieve this?
回答1:
You can share data between Host App
and App Extension
only using App Group
.
There is no way of direct communication between your Host App
and App Extension
.
Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.
App Group
is a shared container
used by both Host App
as well as App Extension
. It is like UserDefaults
that store key-value pairs
.
So you can save your current user information in App Group
from Host App
and then access it in your Today Extension
from the same App Group
.
For more on App Groups
refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
来源:https://stackoverflow.com/questions/44635989/how-to-access-current-firebase-user-from-ios-today-extension