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

故事扮演 提交于 2019-12-31 03:27:08

问题


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.


回答1:


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  {
   ...
}


来源:https://stackoverflow.com/questions/44455433/how-to-use-same-authenticated-user-token-between-main-ios-app-and-its-share-exte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!