Cocoa How to know if a file was edited/closed - objective-c

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");
}

Related

Deleting App from MacOS but not cleared NSUserDefaults data

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

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 write data to an xml file

I want to store data (preferences) into an xml file , but I can't achieve it. I took a look at appledeveloper web but it seems not clear to me .
I want to store the xml file on disk . My preferences that I created are some checkboxes (ih the user cheks the login box for example and I add this preference login to the xml file) , radio group, textfields
can you give me a good tutorial or an example that shows that please?
You usually store preferences using the NSUserDefaults class.
Example:
[[NSUserDefaults standardUserDefaults] setObject:#"Option1" forKey:#"PreferenceName1"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"PreferenceName2"];
And then read like this:
NSString *pref1 = [[NSUserDefaults standardUserDefaults] objectForKey:#"PreferenceName1"];
BOOL pref2 = [[NSUserDefaults standardUserDefaults] boolForKey:#"PreferenceName2"];

How to save a string for later use?

I am currently working on having a file update from a remote server. I am able to download the file and save it to the documents directory. The file has a "Last-Modified" tag and I am using it to check if the file needs to be updated. But my question is, how do you save the string with the tag for later use? Later I want to compare the saved string with the another string with the current "Last-Modified" tag. If they are equal the file doesn't have to be updated but if they're not equal I will download the new file.
Sorry for bad English, correct me and any help is appreciated. Have been struggling with this for a while!
EDIT:
NSDictionary *metaData = [test allHeaderFields];
//NSLog(#"%#", [metaData description]);
lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:lastModifiedString forKey:#"LastModified"];
[standardUserDefaults synchronize];
NSString *savedString = [[NSUserDefaults standardUserDefaults] stringForKey:#"LastModified"];
if (![lastModifiedString isEqualToString:savedString])
{
[self downloadNewFile];
}
Download link to files: Archive.zip
Use NSUserDefaults or Core Data to persist a value.
EDIT:
It is not working because you are saving the new value before retrieving it. You'll need to move
NSString *savedString = [[NSUserDefaults standardUserDefaults] stringForKey:#"LastModified"];
above
[standardUserDefaults setObject:lastModifiedString forKey:#"LastModified"];
Now you'll be comparing the new file value against the old user defaults value.
You are saying you want to compare the Last Modified dates to see if they are the same?
I think the best way to do this would be to save the date (as a string) in a Property List. If you are making an iPhone app you can create a property list with a string with the code below. This code checks if a file already exists and if it does, it reads from it, and if it doesn't, it creates one and writes to it.
// Get the path to the property list.
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToPlist = [[pathArray objectAtIndex:0] stringByAppendingPathComponent:#"yourfilename.plist"];
// Check whether there is a plist file that already exists.
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pathToPlist];
if (!fileExists) {
// There is no file so we set the file up and save it.
NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:1];
[newDict setObject:yourStringWithTheLastModifiedDate forKey:#"lastModified"];
[newDict writeToFile:pathToPlist atomically:YES];
}
} else {
// There is already a plist file. You could add code here to write to the file rather than read from it.
// Check the value of lastModified.
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathToPlist];
NSString *lastModifiedDate = [persistentNonResidentData objectForKey:#"lastModified"];
// Add your own code to compare the strings.
}
Or I may have misunderstood your question and that may not be what you are looking for at all lol.

Save Username & Pass Objective C iPhone

i have a textfield,
Number and Password
i have no clue how to save these settings and then read them as the app starts to check if they have been set or not.
Thanks
Mason
Sensitive information should be stored in the keychain, a cryptographically secure location on the device. If you save the username and/or password in NSUserDefaults, you're saving them as plaintext, which is inherently insecure.
There're are plenty of examples on the internet of how to use the keychain on the iPhone, include simple wrappers to use in your code. For example, here's some pretty good code on Github that makes it quite easy:
http://github.com/ldandersen/scifihifi-iphone/tree/master/security
To Save:
[[NSUserDefaults standardUserDefaults] setValue:_email forKey:#"email"];
[[NSUserDefaults standardUserDefaults] setValue:_password forKey:#"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
To Read:
_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
_password = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
In your case:
[[NSUserDefaults standardUserDefaults] setValue:Number.text forKey:#"Number"];
And:
NSString * number = [[NSUserDefaults standardUserDefaults] stringForKey:#"Number"];
The "Key" is usually hard-coded and works like a "variable name" for things in storage (typical name/value pairs like a dictionary).
To check if the value has been set or not; after you read it:
if (number) ...