【发布时间】:2020-12-25 02:23:57
【问题描述】:
我的问题听起来很简单:我有一个应用程序和一个共享扩展。我想做的是检查共享扩展内部是否有用户登录。为此我找到了documentation。
现在,如果用户登录,我会将登录用户迁移到共享钥匙串(在主应用程序内):
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 的数据。但这是失败的,因为currentUser 是nil...
static func getWishlists(completion: @escaping (_ success: Bool, _ dataArray: Any?, _ dropDownArray: Any?) -> Void) {
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid // <- throws error
...
该函数也在用户登录后立即调用(顶部的第一个代码 sn-p)并且工作正常,但只是第一次。
这是我从 Firebase 获得的error:
6.21.0 - [Firebase/Auth][I-AUT000001] 启动时加载保存的用户时出错:Error Domain=FIRAuthErrorDomain Code=17995“访问钥匙串时发生错误。@c 中的 NSLocalizedFailureReasonErrorKey 字段NSError.userInfo 字典将包含有关遇到的错误的更多信息" UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_KEYCHAIN_ERROR, NSLocalizedFailureReason=SecItemCopyMatching (0), NSLocalizedDescription=访问钥匙串时发生错误。 @c NSError.userInfo 字典中的@c NSLocalizedFailureReasonErrorKey 字段将包含有关遇到的错误的更多信息}
我的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")
}
但即使用户刚刚在主应用程序中登录,也会打印not signed in。
我不知道我在这里到底做错了什么,并且在文档中找不到任何内容。也许这是我为用户检索数据的方式。也许我也需要在那里用accessGroup 打电话,但我被困住了......感谢任何帮助!
【问题讨论】:
-
确认一下,你有钥匙串共享功能,对吧?
-
@Pastre 是的,在具有相同标识符的应用程序和扩展程序上
-
NSError.userInfo字典的NSLocalizedFailureReasonErrorKey是怎么说的? -
@TarunTyagi 我不知道如何阅读该错误...
-
对不起,它在错误日志中可见 - 这部分
NSLocalizedFailureReason=SecItemCopyMatching (0)。这似乎是一个环境/配置问题。请参阅此线程 - github.com/firebase/firebase-ios-sdk/issues/3215 完全相同的错误。
标签: ios swift firebase firebase-authentication keychain