Log out after uninstalling application - Firebase

My application will not exit the current user after the application has been deleted from the phone. I do not want the user to uninstall the application, and when they reinstall it, they are already logged in.

I think this has something to do with his access to the keychain, maybe? Not sure. I was thinking, maybe I just needed to not authenticate the user after uninstalling the application, but there is no way to verify this condition. The applicationWillTerminate function will work closest to this, but if I put my FIRAuth.auth()?.signOut() , it will sign my user every time the application is killed. I do not want it.

How do I do this job?

+7
source share
1 answer

Although there is no function or handler to check when the application was removed from the phone, we can check if the application is the first launch. Most likely, when the application starts for the first time, it also means that it has just been installed and nothing is configured in it. This process will be executed in didfinishLaunchingWithOptions above the line return true .

First, we must set the user defaults:

 let userDefaults = UserDefaults.standard 

After that, we need to check whether the application was launched before or was launched earlier:

 if (!userDefaults.bool(forKey: "hasRunBefore")) { print("The app is launching for the first time. Setting UserDefaults...") // Update the flag indicator userDefaults.set(true, forkey: "hasRunBefore") userDefaults.synchronize() // This forces the app to update userDefaults // Run code here for the first launch } else { print("The app has been launched before. Loading UserDefaults...") // Run code here for every other launch but the first } 

Now we checked whether the application launches for the first time or not. Now we can try to log out of our user. Here's what the updated condition should look like:

 if (!userDefaults.bool(forKey: "hasRunBefore")) { print("The app is launching for the first time. Setting UserDefaults...") do { try FIRAuth.auth()?.signOut() } catch { } // Update the flag indicator userDefaults.set(true, forkey: "hasRunBefore") userDefaults.synchronize() // This forces the app to update userDefaults // Run code here for the first launch } else { print("The app has been launched before. Loading UserDefaults...") // Run code here for every other launch but the first } 

Now we have checked whether the user launches the application for the first time, and if so, log out if the user has previously logged in. All compiled code should look like this:

 let userDefaults = UserDefaults.standard if (!userDefaults.bool(forKey: "hasRunBefore")) { print("The app is launching for the first time. Setting UserDefaults...") do { try FIRAuth.auth()?.signOut() } catch { } // Update the flag indicator userDefaults.set(true, forkey: "hasRunBefore") userDefaults.synchronize() // This forces the app to update userDefaults // Run code here for the first launch } else { print("The app has been launched before. Loading UserDefaults...") // Run code here for every other launch but the first } 
+20
source

Source: https://habr.com/ru/post/1260110/


All Articles