问题
We have the main app integrated with Firebase SDK. User sign in via main application using email, google or facebook.
Now, we have share extension implemented which ideally should share same Authentication session internally so that data can be sent on Firebase with the same user without asking him to login again through the extension.
So, does anyone know the way to share Firebase authentication session between the Main app and share extension?
Either we sent some internal call to the main app to perform Firebase stuff because it has authentication detail within it.
The main app set some token to common user defaults via app groups which will be then used by Share extension to re-authenticate automatically.
Or Firebase provide some way to do so,
I don't know what is feasible from above.
I found a method signInWithCustomToken:completion:
but, it's not related to what I actually looking for.
回答1:
To read and save from the same set of NSUserDefaults you need to the the following:
- In your main app, select your project in the project navigator.
- Select your main app target and choose the capabilities tab.
- Switch on App Groups (this will communicate with the developer portal, as it is generating a set of entitlements, and relevant App Id and so forth).
- Create a new container. According to the help, it must start with “group.”, so give it a name like “group.myapp.test”.
- Select your Today Extension target and repeat this process of switching on app groups. Don’t create a new one, rather select this newly created group to signify that the Today Extension is a member of the group.
Write to your NSUserDefaults:
// In this example I´m setting FirstLaunch value to true
NSUserDefaults(suiteName: "group.myapp.test")!.setBool(true, forKey: "FirstLaunch")
Read from NSUserDefaults:
// Getting the value from FirstLaunch
let firstLaunch = NSUserDefaults(suiteName: "group.myapp.test")!.boolForKey("FirstLaunch")
if !firstLaunch {
...
}
来源:https://stackoverflow.com/questions/44455433/how-to-use-same-authenticated-user-token-between-main-ios-app-and-its-share-exte