2
votes

Inside my app the user can sign in with Apple, Google, Facebook and Email using Firebase. I also have a Share Extension and I would like to share the auth-state from the Main-App so I can also call Auth.auth.currentUser inside my ShareExtension so the user can access Cloud-Firestore.

Now I know there is this documentation provided by Firebase. However I am not sure about Step 2:

do {
   try Auth.auth().useUserAccessGroup("TEAMID.com.example.group1")
} catch let error as NSError {
   print("Error changing user access group: %@", error)
}

Where exactly do I have to call this?

Edit:

Main App:

enter image description here

Share Extension: enter image description here

Update:

It is throwing this error when calling the code above before signIn:

Error changing user access group: %@ Error Domain=FIRAuthErrorDomain Code=17995 "An error occurred when accessing the keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary will contain more information about the error encountered" UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_KEYCHAIN_ERROR, NSLocalizedFailureReason=SecItemCopyMatching (-34018), NSLocalizedDescription=An error occurred when accessing the keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary will contain more information about the error encountered}

1
Inside didFinishLaunchingWithOptions (and similar methods in extensions) I guess. Just after FirebaseApp.configure()Dima Rostopira
@DimaRostopira tried that but when calling let userID = Auth.auth().currentUser!.uid inside my Share Extension it is always nil...Chris
Make sure you use correct group id and enabled app groups for extensionDima Rostopira
@DimaRostopira what exactly do you mean by correct group id ?Chris

1 Answers

0
votes

In the documentation you can see the flow you have to follow. First, anywhere in the app you have the login process you have to add this lines of code:

do {
  try Auth.auth().useUserAccessGroup("TEAMID.com.example.group1")
} catch let error as NSError {
  print("Error changing user access group: %@", error)
}

You can add them in the didFinishLaunchingWithOptions function in the delegate, but you can use it wherever you want.

In the other app you have to add the same code. The two apps have to have the same Access Group, right?

Then in one of the app, you have to execute this sentence:

Auth.auth().signInAnonymously { result, error in
  // User signed in
}

You can put it just after the first code above. Then you will be able to access the user in both apps like this:

var user = Auth.auth().currentUser