ios - 使用共享的 iOS Keychain 启用跨应用程序 Firebase 身份验证

我的问题听起来很简单:我有一个应用程序和一个共享扩展。我想做的是检查共享扩展内部是否有用户登录。为此我找到了这个 documentation .

现在,如果用户登录,我会将登录用户迁移到共享钥匙串(keychain)(在主应用程序内):

   Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
        
        if let userId = result?.user.uid { // successfully signed in
            
            let user = Auth.auth().currentUser
            do {
              try Auth.auth().useUserAccessGroup("teamID.com.example-app.group")
            } catch let error as NSError {
              print("Error changing user access group: %@", error)
            }
            Auth.auth().updateCurrentUser(user!) { error in
                print(error?.localizedDescription as Any)
            }
        }
    }

这没有给我任何错误,所以我猜它正在工作。但我目前面临两个主要问题:

1。主应用程序内部的问题:

就像我说的上面的代码工作得很好一样,我也加载了一些数据,这些数据在用户第一次注册时工作得很好。但是,如果我重新启动应用程序,则用户不必再次登录,并且应该被定向到主视图,我会在主视图中再次为 currentUser 检索数据。但这是失败的,因为 currentUsernil...

static func getWishlists(completion: @escaping (_ success: Bool, _ dataArray: Any?, _ dropDownArray: Any?) -> Void) {
    
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser!.uid // <- throws error
    ...

该函数也在用户登录后立即调用(顶部的第一个代码片段)并且工作正常但只是第一次。

这是我从 Firebase 得到的错误:

6.21.0 - [Firebase/Auth][I-AUT000001] Error loading saved user when starting up: 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 (0), 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}

我的 Share Extension 中的第二个问题:

就像我说的,我想检查用户是否登录。为此,我在 Share Extension 中尝试了这个:

    do {
      try Auth.auth().useUserAccessGroup("teamID.com.example-app.group")
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    
    if Auth.auth().currentUser != nil {
      // User is signed in.
      print("signed in")
    } else {
      // No user is signed in.
      print("not signed in")
    }

但这是打印未登录,即使用户刚刚在主应用程序中登录也是如此。

我不知道我到底做错了什么,也无法在文档中找到任何内容。也许这是我为用户检索数据的方式。也许我还需要用 accessGroup 调用一些东西,但我被卡住了......感谢任何帮助!

最佳答案

我不太确定你的问题,但你不应该更改设置你自己的 userAccessToken

do {
      try Auth.auth().useUserAccessGroup("XXXXXXXXXXXXX")
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    
    if Auth.auth().currentUser != nil {
      // User is signed in.
      print("signed in")
    } else {
      // No user is signed in.
      print("not signed in")
    }

将您的应用程序 userAccessGroup 替换为我放置 XXXXXXXXXXXXX 占位符的位置。

https://stackoverflow.com/questions/63764437/

相关文章:

python - 在pygame中的数组支持网格中交换颜色的问题

python - Pygame 全屏放大

r - “Enter”键不会与 splashR::splash_send_key 一起发送

api - Google Scripts 应用程序和 Plaid 链接身份验证

css - 如何将边框颜色作为线性渐变放在表格行边框上?

python - 在 Web 应用中使用 SAML 实现 Azure AD 身份验证

python - "An error occurred initiating application

python - Plotly 多类别 X 轴排序

reactjs - reactstrap Collapse 没有出现在手机上

python - 未处理的异常 : Connection closed while receivin