Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中注销Firebase_Swift_Firebase Authentication - Fatal编程技术网

在Swift中注销Firebase

在Swift中注销Firebase,swift,firebase-authentication,Swift,Firebase Authentication,我正试图注销Firebase API,但我似乎不知道如何处理可能发生的任何错误 Firebase pod提供了一种注销方法: FIRAuth.auth()?.signOut() 它被标记为抛出,因此我将它包装在一个执行/尝试/捕获块中,以测试注销过程: do{ 请尝试FIRAuth.auth()?.signOut() }捕捉(让错误){ 打印((错误为NSError).code) } 我看到signOut方法在Firebase pod中标记为throws,但我不知道它如何异步处理任何错误。我

我正试图注销Firebase API,但我似乎不知道如何处理可能发生的任何错误

Firebase pod提供了一种注销方法:

FIRAuth.auth()?.signOut()
它被标记为
抛出
,因此我将它包装在一个
执行
/
尝试
/
捕获
块中,以测试注销过程:

do{
请尝试FIRAuth.auth()?.signOut()
}捕捉(让错误){
打印((错误为NSError).code)
}
我看到
signOut
方法在Firebase pod中标记为
throws
,但我不知道它如何异步处理任何错误。我曾尝试进入飞行模式,这会在网络请求发生的任何地方触发代码中的网络错误,但使用
注销
方法,该错误不会被捕获,因为我没有可执行的完成处理程序。Firebase吊舱中的所有其他身份验证方法都有一个完成处理程序,我可以在其中处理错误

以下是Firebase pod的
注销方法的文档:

/**@fn注销:
@简短地签出当前用户。
@可选参数错误;如果发生错误,返回时包含一个NSError对象
描述问题;否则为零。
@当注销请求成功时返回@YES@没有别的。
@备注:可能的错误代码:
-@c FIRAuthErrorCodeKeychainError表示访问密钥链时发生错误。
@c NSError.userInfo字典中的@c NSLocalizedFailureReasonErrorKey字段
将包含有关遇到错误的详细信息。
*/
open func signOut()抛出

当我没有一个允许我检查错误的完成处理程序时,您对如何处理用户注销有什么建议吗?

您可以像这样捕获错误

do
{
     try Auth.auth().signOut()
}
catch let error as NSError
{
     print(error.localizedDescription)
}

您可以像这样捕捉错误

do
{
     try Auth.auth().signOut()
}
catch let error as NSError
{
     print(error.localizedDescription)
}

错误不太可能发生,但假设什么都不好。通过文档的声音,它清除了您的钥匙链,这是您能够重新登录firebase应用程序的唯一方法。尝试注销我自己的firebase应用程序时,我惊讶地发现出现了0个错误。这是原始代码

 @IBAction func logOutTapped(_ sender: Any) {

    let firebaseAuth = FIRAuth.auth()
    do {
        try firebaseAuth?.signOut()
    } catch let signOutError as NSError {
        print ("Error signing out: %@", signOutError)
    }

    if Utility.hasFacebook {
        let login = FBSDKLoginManager()
        login.logOut()
    }

    if Utility.hasTwitter {
        Twitter.sharedInstance().sessionStore.logOutUserID((Twitter.sharedInstance().sessionStore.session()?.userID)!)
    }
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC") 

    self.present(initialViewController, animated: false)
}
不管怎样,如果你真的想要一个完成处理程序,那么这里有一些我很快就抛出的东西

  func logOut(completion:@escaping(_ errorOccured: Bool) -> Void)  {
    let firebaseAuth = FIRAuth.auth()
    do {
        try firebaseAuth?.signOut()

    } catch let signOutError as NSError {
        completion(true)
    }

    completion(false)


}

错误不太可能发生,但假设什么都不好。通过文档的声音,它清除了您的钥匙链,这是您能够重新登录firebase应用程序的唯一方法。尝试注销我自己的firebase应用程序时,我惊讶地发现出现了0个错误。这是原始代码

 @IBAction func logOutTapped(_ sender: Any) {

    let firebaseAuth = FIRAuth.auth()
    do {
        try firebaseAuth?.signOut()
    } catch let signOutError as NSError {
        print ("Error signing out: %@", signOutError)
    }

    if Utility.hasFacebook {
        let login = FBSDKLoginManager()
        login.logOut()
    }

    if Utility.hasTwitter {
        Twitter.sharedInstance().sessionStore.logOutUserID((Twitter.sharedInstance().sessionStore.session()?.userID)!)
    }
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC") 

    self.present(initialViewController, animated: false)
}
不管怎样,如果你真的想要一个完成处理程序,那么这里有一些我很快就抛出的东西

  func logOut(completion:@escaping(_ errorOccured: Bool) -> Void)  {
    let firebaseAuth = FIRAuth.auth()
    do {
        try firebaseAuth?.signOut()

    } catch let signOutError as NSError {
        completion(true)
    }

    completion(false)


}

根据米莉的回答进行编辑,将发送用户添加回应用程序的初始页面

// log out

func logout(){
    do
    {
        try Auth.auth().signOut()
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let IntroVC = storyboard.instantiateViewController(withIdentifier: "IntroVC") as! introVC
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = IntroVC
    }
    catch let error as NSError
    {
        print(error.localizedDescription)
    }


}

根据米莉的回答进行编辑,将发送用户添加回应用程序的初始页面

// log out

func logout(){
    do
    {
        try Auth.auth().signOut()
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let IntroVC = storyboard.instantiateViewController(withIdentifier: "IntroVC") as! introVC
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = IntroVC
    }
    catch let error as NSError
    {
        print(error.localizedDescription)
    }


}