skip to Main Content

Problem:
While developing my Flutter app for iOS, I encountered an issue where, if a user uninstalls the app while logged in, upon reinstalling, the app would skip the login screen and directly navigate to the home screen. This was happening because sensitive data like the access token was stored in the Keychain, which persists even after the app is uninstalled.

Here’s the AppDelegate.swift file:

import Flutter
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Solution: To resolve this, I added logic to clear the Keychain when the app is installed for the first time after being reinstalled. Here’s the updated AppDelegate.swift file:

    import Flutter
    import UIKit
    import Security
    
    @main
    @objc class AppDelegate: FlutterAppDelegate {
    
        func clearKeychainIfReinstalled() {
            let defaults = UserDefaults.standard
            let alreadyInstalled = defaults.bool(forKey: "has_installed")
    
            if !alreadyInstalled {
                clearKeychain()
                defaults.set(true, forKey: "has_installed")
                defaults.synchronize()
            }
        }
    
        func clearKeychain() {
            let secItemClasses = [
                kSecClassGenericPassword,
                kSecClassInternetPassword,
                kSecClassCertificate,
                kSecClassKey,
                kSecClassIdentity
            ]
            for itemClass in secItemClasses {
                let query = [kSecClass: itemClass]
                SecItemDelete(query as CFDictionary)
            }
        }
    
        override func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
            clearKeychainIfReinstalled()
            GeneratedPluginRegistrant.register(with: self)
            return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
    }
    

    Explanation:

    1. Persistent Installation Check: I used UserDefaults to check if the app has been previously installed.
    2. Clear Keychain: If the app is being installed for the first time after a reinstall, the clearKeychain function is called, which deletes all Keychain items.
    3. Keychain Classes: The clearKeychain function iterates over various Keychain classes (kSecClassGenericPassword, kSecClassInternetPassword, etc.) and removes all stored items.

    Outcome: This ensures that sensitive data is cleared upon reinstallation, and the app starts fresh, taking the user to the login screen instead of directly navigating to the home screen.


  2. check the credentails and app info remove the local storage and cached some time cached are saved then this issue are persisted but ios build uninstalled not saved the credentails but please check the double crosss checking

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search