Search code examples
swiftfirebasefirebase-authenticationgoogle-cloud-firestoreshare-extension

Is there a way to use Firebase with a ShareExtension without AppGroups


I'm setting up a shareExtension in iOS and want to use the FirebaseSDK to upload data direct instead of using AppGroups. This works as expected, but after 1 hour the UserToken get's invalidated and i can't reach the Firestore Backend anymore.

I'm using the FirebaseSDK (6.2.0) and enabled Keychain sharing to access the current signedIn User. I have the same Google-Plist in the MainApp and the shareExtension. The data gets also uploaded correctly from the shareExtension and was also updated via the snapshotListener in the MainApp.

Relevant code in the MainApp

    lazy var db = Firestore.firestore()
    //TEAMID form the Apple Developer Portal
    let accessGroup = "TEAMID.de.debug.fireAuthExample"

    override func viewDidLoad() {
        super.viewDidLoad()
        do {
            try Auth.auth().useUserAccessGroup("\(accessGroup)")
        } catch let error as NSError {
            print("Error changing user access group: %@", error)
        }
        guard let user = Auth.auth().currentUser else {
            self.statusLabel.text = "user get's lost"
            return
        }
        statusLabel.text = "UserID: \(user.uid)"
        // Do any additional setup after loading the view.
        db.collection("DummyCollection").addSnapshotListener { (querySnapshot, error) in
            if let err = error {
                print(err.localizedDescription)
            }
            guard let snapshot = querySnapshot else {
                return
            }
            DispatchQueue.main.async {
                self.dbCountLabel.text = "\(snapshot.count)"
            }
        }

    }

        func signIN(){
        // https://firebase.google.com/docs/auth/ios/single-sign-on
        do {
            try Auth.auth().useUserAccessGroup("\(accessGroup)")
        } catch let error as NSError {
            print("Error changing user access group: %@", error)
        }
        Auth.auth().signInAnonymously { (result, error) in
            if let err = error{
                print(err.localizedDescription)
                return
            }
            print("UserID: \(Auth.auth().currentUser!.uid)")
            }
        }
    }
 }  

Code in the shareExtension:

override func viewDidLoad() {
        super.viewDidLoad()

        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }
        do {
            try Auth.auth().useUserAccessGroup(accessGroup)
        } catch let error as NSError {
            print("Error changing user access group: %@", error)
        }
        tempUser = Auth.auth().currentUser

        if tempUser != nil {
            userIDLabel.text = "UserID: \(tempUser!.uid)"
            doneButton.isEnabled = true

            db.collection("DummyCollection").addSnapshotListener { (querySnapshot, error) in
                if let err = error {
                    print(err.localizedDescription)
                }
                guard let snapshot = querySnapshot else {
                    return
                }
                DispatchQueue.main.async {
                    self.dataCountLabel.text = "\(snapshot.count)"
                }
            }
        } else {
            // No user exists in the access group
            self.navigationItem.title = "No User"
        }
    }

I expect that this should be possible, but the Token gets somehow invalid in the MainApp and i could not reach the Firestore backend.

6.2.0 - [Firebase/Auth][I-AUT000003] Token auto-refresh re-scheduled in 01:00 because of error on previous refresh attempt.

6.2.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information.


Solution

  • Answering my own question: This should be fixed in the next release (Firebase 6.4.0) Details can be found in this PR 3239.