简体   繁体   中英

How to use same Authenticated user token between main iOS app and its Share Extension

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?

  1. Either we sent some internal call to the main app to perform Firebase stuff because it has authentication detail within it.

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

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

To read and save from the same set of NSUserDefaults you need to the the following:

  1. In your main app, select your project in the project navigator.
  2. Select your main app target and choose the capabilities tab.
  3. 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).
  4. Create a new container. According to the help, it must start with “group.”, so give it a name like “group.myapp.test”.
  5. 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  {
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM