有没有办法在不带AppGroups的情况下将Firebase与ShareExtension一起使用

时间:2019-06-07 07:50:57

标签: swift firebase firebase-authentication google-cloud-firestore share-extension

我正在iOS中设置shareExtension,并希望使用FirebaseSDK直接上传数据,而不是使用AppGroups。这可以按预期工作,但是1个小时后UserToken失效了,我再也无法到达Firestore后端了。

我正在使用FirebaseSDK (6.2.0)并启用了钥匙串共享来访问当前的SignIn用户。我在MainApp和Google-Plist中有相同的shareExtension。还可以从shareExtension正确上传数据,并通过MainApp中的snapshotListener更新数据。

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)")
            }
        }
    }
 }  

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"
        }
    }

我希望这是可能的,但是令牌在MainApp中变得无效,并且我无法到达Firestore后端。

6.2.0-[Firebase / Auth] [I-AUT000003]由于先前的刷新尝试出错,令牌自动刷新时间在01:00进行了重新安排。

6.2.0-[Firebase / Firestore] [I-FST000001]无法访问Cloud Firestore后端。连接失败1次。最新错误:发生内部错误,请打印并检查错误详细信息以获取更多信息。

1 个答案:

答案 0 :(得分:0)

回答我自己的问题:在下一版本(Firebase 6.4.0)Details can be found in this PR 3239中应解决此问题。