Deleting App from MacOS but not cleared NSUserDefaults data - objective-c

I have one.dmg file, installed it one time and some data saved in NSUserDefaults. But When I am remove from application as well Trash its not cleared of that NSUserDefaults. I want to remove all of that.

You use the code in applicationWillTerminate callback:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
https://stackoverflow.com/a/29653178/2837760

Related

NSUserDefaults is not saving any value in cocoa [duplicate]

This question already has answers here:
Mac sandbox created but no NSUserDefaults plist
(5 answers)
Closed 5 years ago.
I am trying to save BOOL value and some simple string using NSUserDefaults. But NSUserdefaults is not saving any value. I have also used synchronize after saving the value into NSUserDefaults.The way I am using to save the BOOL into NSUserDefaults is as below.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"loadfirsttimewindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
And the way I am using to save the string into NSUserDefaults is as below.
[[NSUserDefaults standardUserDefaults] setValue:title1 forKey:#"item1"];
[[NSUserDefaults standardUserDefaults] synchronize];
So please let me know that where my NSUserDefaults value has been stored and why it is not saving any value into NSUserDefaults.
Simply I just want to save value which will remain saved even after the application has been closed.
The sample of code I have used for saving the value into NSUserDefaults in given below:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"%hhd",[[NSUserDefaults standardUserDefaults] boolForKey:#"loadfirsttimewindow"]);
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"loadfirsttimewindow"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"loadfirsttimewindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
*I have written code for the thing I have to do only when the application is launched for the first time.*
}
else
{
*I have written code for the thing I have to after the application is launched for the first time.*
}
}
I have found my solution from the link given below:
Mac sandbox created but no NSUserDefaults plist
When you move the container while testing / debugging to the trash, the cfprefsd (see Activity Monitor) still keeps a link to the .plist. Empty the trash and force quit both cfprefsd (user and root).

How to remove all NSUser Defaults except two objects?

I am using a lot NSUserDefaults and i want every time the app starts to remove them except two objects is this somehow possible?
Any help appreciated
I assume you know which two objects you want to keep? If that's the case use this code:
id obj1 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj1"];
id obj2 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj2"];
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] setObject:obj1 forKey:#"keyForObj1"];
[[NSUserDefaults standardUserDefaults] setObject:obj2 forKey:#"keyForObj2"];
You would need to keep track of the keys yourself and remove the objects associated with them on launch. More importantly, though: why are you storing data in NSUserDefaults that you only want to persist through a single run of the app? That sort of data should probably be kept in memory.
You can save Those two items under separate a key and Add into the Code during ViewDidLoad.
Let me know the Actual Problem.

Switching between NSUserDefaults

Maybe somebody will know, how to "tag" NSuserdefaults.
I have like 10 user defaults:
name
pin
etc...
I want to make another object, which would have the same defaults as the other ones (same variables but different values). Like a version, if object == 1 then load one userdefaults and if object == 0 another ones. But how to make it done?
Tried to make something like this
[NSUserDefaults setVersion:object.intValue];
But i guess this isn't the way to do it. So maybe anyone could help how to make it done?
To be more specific a simple example would help like how to do this:
Object (1 or 0)
[[NSuserDefaults standartUserDefaults] setObject: #"something"
forKey: #"Name"];
[[NSUserDefaults standartUserDefaults] synchronize];
NSString *name = [[NSUserDefaults standartUserDefaults] stringForKey:"#name"];
How to set and get this Name depending on Object value?
You could use persistent domains.
NSMutableDictionary *defaults = [[[NSUserDefaults standardUserDefaults] persistantDomainForName:#"aString"] mutableCopy];
// make changes to the dictionary here
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:#"aString"];
[[NSUserDefaults standardUserDefaults] setPersistantDomain:defaults forName:#"aString"];
Note that you'll need to be accessing all the defaults via the dictionary if you do this.

Cocoa How to know if a file was edited/closed

I open a file programmatically with the default application taking into account the file extension.
When the file is open, I need to know when the file is modified and when is no longer in use.
I used FSEventStreamCreate to create the event.
How can I know if the file was closed?
Thanks,
Ana
You can check the modified date of the file.
Here is the solution to see if my file was modified:
NSString * modified = [NSString stringWithFormat:#"%#",[fileAttributes objectForKey:#"NSFileModificationDate"]];
NSString * savedmod = [[NSUserDefaults standardUserDefaults] objectForKey:#"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] setObject:modified forKey:#"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (![savedmod isEqualToString:modified])
{
NSlog(#"File modified");
}

NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?

Struggling with this problem and loath to implement a custom cookie management system.
It appears some hidden level of iOS's implementation of HTTP fails to manage sessionless cookies properly. Any time an HTTP response sets or deletes a cookie, immediate inspection of NSHTTPCookieStorage cookies will yield the expected results and indicate the correct sessionOnly value.
But if the app quits soon after a response updates cookies, upon relaunch those sessionOnly=FALSE cookies will be reverted to some previous state and the most recent updates lost.
Whether the cookies are set/deleted by a response header or NSHTTPCookieStorage setCookie: makes no difference.
Some caching/syncing voodoo must be going on behind the scenes. The time it takes for the cookie to become persistent can be up to 5 seconds.
ANYONE out there who has or can point to some definitive explanation of this behavior? Is it a bug, plain and simple? Or some undocumented feature whose purpose I can't comprehend?
Some code you can use to reproduce:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie *cookie;
for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
NSLog(#"%#=%#", cookie.name, cookie.value);
}
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:#"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:[NSString stringWithFormat:#"%f", [[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue];
[cookieProperties setObject:#"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:#"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:#"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:#"0" forKey:NSHTTPCookieVersion];
// set expiration to one month from now
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
This code should output a new value on every launch. Instead you will see that if you quit the app quickly the value is unchanged.
Some possibly related stack overflow questions:
iphone NSHTTPCookieStorage avaible on app reopen?
iPhone: NSHTTPCookie is not saved across app restarts
NSHTTPCookies refuse to be deleted
deleted NSHTTPCookie returns if app is terminated
I think the answer lies in one of the SO posts linked to in your question:
I made a sample project to reproduce this issue — and found that it
would only occur when the app receives a SIGKILL signal, like when the
debugger is stopped from within Xcode. In my experiments, unhandled
exceptions, crashes, exit() and abort() don't cause
NSHTPPCookieStorage to loose data.
As this looks like a debugging-only issue (it only occurs when using
the debugger), I closed the radar I filled previously.
You can test this by restarting the phone normally and observing that all changes to NSHTTPCookieStorage are correctly persisted and reloaded.
I also got the same problem but i found a solution. I saved the cookies as it get created by the browser and then recreate them as app restarts.
1) Save cookie when they get created by uiwebview.
NSMutableArray *cookieArray = [[NSMutableArray alloc] init];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[cookieArray addObject:cookie.name];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
[cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
[cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
[cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
[cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion];
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
[[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:#"cookieArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
2) Now recreate them as app restarts:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray* cookieDictionary = [[NSUserDefaults standardUserDefaults] valueForKey:#"cookieArray"];
NSLog(#"cookie dictionary found is %#",cookieDictionary);
for (int i=0; i < cookieDictionary.count; i++)
{
NSLog(#"cookie found is %#",[cookieDictionary objectAtIndex:i]);
NSMutableDictionary* cookieDictionary1 = [[NSUserDefaults standardUserDefaults] valueForKey:[cookieDictionary objectAtIndex:i]];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
// other code
}
thanks
Sounds like an underlying NSUserDefaults-style "synchronize" call is required. I think your best shot is managing all your app's cookies separately in the standard NSUserDefaults and synching any missing ones into the NSHTTPCookieStorage's at startup. Or see if there's some private synchronize method if you're feeling brave :)