LogOut FaceBook, Google & UserDefault Swift 4



Use Button and copy all code

     @IBAction func logOutButton(_ sender: Any) {
        GIDSignIn.sharedInstance().signOut()
        FBSDKLoginManager().logOut()
        UserDefaults.standard.removeObject(forKey: "userToken")
        let vc = LoginViewController()
        self.navigationController?.setViewControllers([vc], animated: true)
        UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
        UserDefaults.standard.synchronize()
    }


NSUserDefaults is likely one of the most used APIs on iOS providing a quick and easy way to store basic configuration and settings. However, these can quickly mount up and you’ll typically end up with something like this in your project within a logout method:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("Username")
defaults.removeObjectForKey("Token")
defaults.removeObjectForKey("SomeSetting")
defaults.removeObjectForKey("FavouriteArtist")
defaults.synchronize()

No comments: