시뮬레이터에서 앱 제거 후 NSUserDefaults가 지워지지 않음
이것은 진짜 NOOB처럼 들릴 수 있습니다! 사용자가 두 번째로 내 응용 프로그램을 입력하는지 확인하여 사용중인 실행 횟수를 유지합니다 NSUserDefaults
. 내에서 다음 코드 구현 rootViewController
의 viewDidLoad
방법 :
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSLog(@"hello %ld", (long)[userDefaults integerForKey:@"runCount"]);
if ([userDefaults integerForKey:@"runCount"] != 1) {
//not the 2nd run
[userDefaults setInteger:1 forKey:@"runCount"];
NSLog(@"not 2nd run");
} else {
//second run or more
NSLog(@"2nd run");
}
[userDefaults synchronize];
모든 것이 잘 작동하지만 문제는 여기 와 여기 에 따라 응용 프로그램을 제거 (삭제 및 다시 설치) 하면 데이터가 지워 져야한다는 것입니다.하지만 앱을 다시 설치 한 후에도 이전 데이터가 여전히 표시됩니다. xCode6-beta를 사용하고 iOS 8에서 애플리케이션을 대상으로하는 iOS 시뮬레이터에서 내 앱을 실행하고 있습니다.
iOS8 베타 시뮬레이터의 버그 때문이라고 생각합니다.
예상되는 동작은 앱이 삭제되면 해당 앱의 NSUserDefaults도 삭제되는 것입니다.
- 그러나 시뮬레이터에서 앱을 제거 할 때 NSUserDefaults는 삭제 되지 않습니다 .
- iOS8을 실행하는 물리적 장치에서 삭제하면 올바르게 삭제됩니다.
현재 빠르고 성가신 해결책은 iOS 시뮬레이터-> 콘텐츠 및 설정 재설정을 클릭하는 것입니다.
Simulator 10이 포함 된 Xcode 9.2는 여전히이 문제를 나타냅니다. 메뉴 옵션은 이제 하드웨어입니다 .. 모든 컨텐츠 및 설정 지우기
버그 보고서를 제출했습니다 btw
콘텐츠 및 설정 재설정은 핵 옵션이므로 iOS 8 / Xcode 6 GM 시뮬레이터의 버그가 해결 될 때까지 두 가지 다른 옵션을 고려할 수 있습니다.
NSUserDefaults
이 저장된 plist 파일을 수동으로 삭제할 수 있습니다 . 이것은 현재~/Library/Developer/CoreSimulator/Devices/*some_device_id*/Library/Preferences/com.yourcompany.yourapp.plist
UUID 디렉토리 이름 중에서 작동하기에 적합한 시뮬레이터를 찾는 것이 약간 지루합니다. 수정 : 2014-10-28 20-34-52 올바른 경로 :~/Library/Developer/CoreSimulator/Devices/*some_device_id*/data/Library/Preferences/com.yourcompany.yourapp.plist
다음을 사용하여 (아마 실행 스크립트 빌드 단계를 사용하여) 그 PLIST에 "수술"을 수행 할 수 plistbuddy 예를
/usr/libexec/plistbuddy -c "Set :BSDidMoveSqliteDb 0" path_to_plist
코드는 장치에서 잘 작동합니다. 시뮬레이터에 버그가있을 수 있습니다.
시뮬레이터의 내용 및 설정을 재설정 해보십시오.
그것은 버그이며 다음 코드로 NSUserDefaults를 삭제할 수 있습니다.
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
이것은 여전히 버그이지만 또 다른 옵션은 NSUserDefaults에서 특정 키를 제거하는 것입니다. 대부분의 경우 테스트 / 개발할 때 NSUserDefaults의 모든 것이 아닌 몇 가지 키에만 관심이 있습니다. 내가 제안하는 것보다 몇 가지 키에만 관심이 있다면 다음을 추가하십시오 removeObjectForKey
.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// ADD THIS TO SIMULATE CLEAN/EMPTY DEFAULTS, COMMENT OUT AFTER INITIAL EXECUTION.
// This will cause the TRUE case to be executed.
[userDefaults removeObjectForKey:@"runCount"]
NSLog(@"hello %ld", (long)[userDefaults integerForKey:@"runCount"]);
if ([userDefaults integerForKey:@"runCount"] != 1) {
//not the 2nd run
[userDefaults setInteger:1 forKey:@"runCount"];
NSLog(@"not 2nd run");
} else {
//second run or more
NSLog(@"2nd run");
}
[userDefaults synchronize];
추가 removeObjectForKey
는 앱의 첫 번째 실행을 시뮬레이션하고 주석 처리하면 모든 후속 앱 실행을 시뮬레이션합니다.
제 경우에는 다음 디렉토리에서 * .plist를 찾았습니다.
[1] / Users / SOME-USERNAME / Library / Developer / CoreSimulator / Devices / SOME-DEVICE-ID /data/Library/Preferences/SP.UserDefaultsTest.plist
문제 : xCode 6 (iOS 8 시뮬레이터)에서 앱을 삭제했지만 파일은 위에서 언급 한 것처럼 디스크에 남아 있습니다.
Solution: Deleting the located file from path [1] manually and the NSUserDefaults are gone.
So the annoying way to reset the simulator is no longer necessary.
For anyone facing the same issue.
If you have more than 1 app under the same group and all of them are using app groups (ON under capabilities), then you will have to remove all the apps from the device in order for the user defaults to be cleared.
Since the user defaults are shared, even if one of the app is on the device then it will not be deleted, as that app will be using the userdefaults.
'program tip' 카테고리의 다른 글
Python : 튜플 / 사전을 키, 선택, 정렬 (0) | 2020.08.30 |
---|---|
Sublime Text에서 전체 JS 자동 완성 얻기 (0) | 2020.08.30 |
주어진 범위에서 모든 숫자의 XOR 찾기 (0) | 2020.08.30 |
API를 사용하여 Instagram에 사진을 게시하는 방법 (0) | 2020.08.30 |
C # vs F # 또는 F # vs C # 사용의 이점은 무엇입니까? (0) | 2020.08.30 |