iOS 17 - UserDefaults getting cleared on relaunching the app

1k views Asked by At

works good on iOS 16 and prior, but on iOS 17, User Defaults gets cleared.

I don't use Data Protection Entitlement. I use app groups, and tried both UserDefaults.standard and UserDefaults(suiteName:"group.com.aaa").

On a new project it works fine, but in my mature project (in AppStore for over 10 years) User Defaults get refreshed.

What changed from iOS 16 to iOS 17?

tried both UserDefaults.standard and UserDefaults(suiteName:"group.com.aaa") and it doesn't work

tried new project and it works tried keychain and it works (but it's too much work to migrate all user defaults data to keychain)

2

There are 2 answers

1
Hua-Ying On
0
Medhi On

Create a new PrivacyInfo.xcprivacy from "File>New" with Xcode 15+ then replace the content of the file in source code mode (right click) with:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSPrivacyAccessedAPITypes</key>
    <array>
        <dict>
            <key>NSPrivacyAccessedAPIType</key>
            <string>NSPrivacyAccessedAPICategoryUserDefaults</string>
            <key>NSPrivacyAccessedAPITypeReasons</key>
            <array>
                <string>CA92.1</string>
            </array>
        </dict>
    </array>
</dict>
</plist>

Here the documentation: https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api