The values stored in NSUserdefault still exist after resinatll the app!!!

The problem only happened on iOS system is 18.3. We save the data about user after user login,so when user login successful ,we save users info in NSUserdefault ,then user delete app,and reinstall the app,when launch the app,it is already logged in.

Answered by DTS Engineer in 825812022

I’m not aware of any change in this space, and I tried this today and things worked as expected. That is, the standard user defaults are stored in your app’s container which is deleted when the app is deleted.

Here’s what I did:

  1. Using Xcode 16.2, I created a new project from the iOS > App template.

  2. I tweaked the code as shown below.

    IMPORTANT This is not good SwiftUI code. It’s just a quick hack to run this test.

  3. I selected an iOS 18.3.1 device as the run destination and chose Product > Run. The app showed “-” because the user default has never been set.

  4. In the app, I tapped Set. Note that the text doesn’t update. See, I told you it wasn’t good SwiftUI code (-:

  5. In Xcode, I chose Product > Stop.

  6. And then Product > Run. This time the app showed the timestamp for when I tapped Set in step 4.

  7. I chose Product > Stop.

  8. On the device’s Home screen, I deleted the app.

  9. I chose Product > Run again. The app showed “-”.

I’m not sure why you’re seeing different behaviour. I encourage you to run through the test above to reproduce my result:

  • If you see different behaviour with my test, let me know.

  • If my test behaves as expected, you should start comparing your code to my test to see where things diverge different.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

struct ContentView: View {
    var body: some View {
        VStack {
            Text(UserDefaults.standard.string(forKey: "test") ?? "-")
            Button("Set") {
                UserDefaults.standard.set("\(Date.now)", forKey: "test")
            }
        }
        .padding()
    }
}

I’m not aware of any change in this space, and I tried this today and things worked as expected. That is, the standard user defaults are stored in your app’s container which is deleted when the app is deleted.

Here’s what I did:

  1. Using Xcode 16.2, I created a new project from the iOS > App template.

  2. I tweaked the code as shown below.

    IMPORTANT This is not good SwiftUI code. It’s just a quick hack to run this test.

  3. I selected an iOS 18.3.1 device as the run destination and chose Product > Run. The app showed “-” because the user default has never been set.

  4. In the app, I tapped Set. Note that the text doesn’t update. See, I told you it wasn’t good SwiftUI code (-:

  5. In Xcode, I chose Product > Stop.

  6. And then Product > Run. This time the app showed the timestamp for when I tapped Set in step 4.

  7. I chose Product > Stop.

  8. On the device’s Home screen, I deleted the app.

  9. I chose Product > Run again. The app showed “-”.

I’m not sure why you’re seeing different behaviour. I encourage you to run through the test above to reproduce my result:

  • If you see different behaviour with my test, let me know.

  • If my test behaves as expected, you should start comparing your code to my test to see where things diverge different.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

struct ContentView: View {
    var body: some View {
        VStack {
            Text(UserDefaults.standard.string(forKey: "test") ?? "-")
            Button("Set") {
                UserDefaults.standard.set("\(Date.now)", forKey: "test")
            }
        }
        .padding()
    }
}
The values stored in NSUserdefault still exist after resinatll the app!!!
 
 
Q