Save a value to disk XCode - ios

I've got a pretty simple task that I'm trying to accomplish in Objective-C.
I make a call to a web site, and get a value from the web site through an HTTP call. I want to save this value to disk so I can retrieve it later.
What would be the best method to do this - a text file, or in the PList file?
It's just one value that may occasionally be updated. The call to the web site is made on-demand.

Consider using NSUserDefaults for storing single values
For Storing:
[[NSUserDefaults standardUserDefaults] setInteger:100 forKey:#"storageKey"];
For Retrieving:
NSInteger myValue = [[NSUserDefaults standardUserDefaults] integerForKey:storageKey];

Related

NSUserDefaults in iOS is randomly disappearing and reappearing

Just wondering if anybody experience this issue?
I am developing an application in iOS using Objective-C at the moment.
Sometimes my data in NSUserDefaults will be missing after I compile the app.
But if I ignore it and recompile the app again the data suddenly reappears.
I already synchronized in several places (not in every key, but only in several places).
If anyone happened to face this issue before I hope you can share how to handle this issue.
P.S. I need a storage to save 1 particular object so I can retrieve it when the app reopens.
Edited to add the code
NSString *enPIN = [[NSString alloc]initWithString:[NSString stringWithFormat:#"%#", [enterField.text md5]]];
[[NSUserDefaults standardUserDefaults] setObject:enPIN forKey:#"pin"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"check pin %#", [[NSUserDefaults standardUserDefaults] objectForKey:#"pin"]);
The object is a string, i hash it using md5 and then store it in nsuserdefault, if it only randomly dissapearing maybe its not weird, but its also reapearing again after it dissapear if i recompile the apps
Check if your defaults are using only string value or bool or such.
If you are using any Object with Class (key and parameters) like NSObject to store in defaults I prefer you do the encoding and decoding accordingly before storing and retrieving the values.
Also If you storing any NSDictionary check if any of the object value inside that dictionary is not anything other than Bool, String , if there also any NSObject class or reference is stored then you may face same issue.
Refer this stackoverflow link as to how encode objects before storing to NSUserDefaults.
Lastly [defaults synchronise] call mandatory on viewwilldisappear or immediately after storing new value whichever way is your implementation.
Hope this helps.
I had a similar issue the other day with NSUserDefaults
Not quite sure what was causing it, but it was due to a bug in Xcode. I was able to fix the issue without changing my code at all. I simply cleaned the project (CMD-Shift-K) and restarted my computer, and then it worked just fine. It's worth a try
Are you getting any kind of error messages in the console?

How can I protect integer data on NSUserDefaults from hacking?

I have the paid content on my application on App Store. And I save it using the following code:
paidContentCount = [[NSUserDefaults standardUserDefaults] integerForKey:#"paidContentCount"];
But it's easy to hack it. How can I protect integer data from hacking?
Never store such settings in NSUserDefaults! Use an iOS Keychain for it. Here is the wrapper, which makes the biggest part of work.
Besides it, you can read this article from Ray Wenderlich - there are a lot of good ideas of basic security.
You can try saving your content as string with [NSString hash] method, and after your app is loaded, compare paid content's hashes with value from NSUserDefaults
UPDATE: forgot that [NSString hash] returns NSInteger

If once value assigned to a variable in objective c while application install then how to access that value at every time of opening an application?

Hi i am new to objective C.
If once value assigned to a variable in objective c while application install then how to access that value at every time of opening an application? And i tried with extern, static. Each will assign and sets the value at first time. If i rerun the application in emulator, it is not taking that last assigned value.
If anything is possible other than File system storage ? Is it possible with static or extern variables.
Scenario:
While install "extern int test" is assigned to 10 then it changed to 20. While accessing from another class, test reflects 20. If i rerun the app, "test" is showing "10"
But i want to access the last assigned the value (like static in java)
Thanks for any help !
Use NSUserDefaults to set the value in AppDelegate :
NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:20 forKey:#"abc"];
[defaults synchronize];
TO get the value again, Use this method :
NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
var = [defaults integerForKey:#"abc"];
After closing the app the data stored in variables do not persist (of all classes), so you need to use NSUserDefaults (in your case)
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs.
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database.
user's defaults database persists when app is killed.
Use NSUSerDefaults value will remain saved until application is deleted
[[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:key1];
[[NSUserDefaults standardUserDefaults] synchronize];
and access value like
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id obj = [defaults objectForKey:key1];
Because all variables of Objective C program are stored in memory, none of them survives application termination. If you need the value to be available after your app restarts, you have several options:
Use the file system - this one is the most straightforward thing to do. Cocoa provides APIs for writing strings to files in a single go, so your code would be short and simple.
Use user defaults - This API helps you persist values for reuse in an organized way.
Use keychain APIs - This API lets you save small amounts of data that must be encrypted. This is probably an overkill in your scenario.
All the variables are store in Memory once will restart your application the memory is released by ARC and allocated by new one memory address
Can user FileSystem or NSUserDefaults in objective c

Best way to store user information for my iOS app

What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.

Dynamic In-App Settings

I have an app where Location is important. Currently I have a multi-value setting in the settings bundle where I have 5 locations defined. The problem with this approach is that the settings bundle is static - i.e. I cannot update that from a JSON list on my server as far as I know.
I want to update the location list from a dynamic list on the server.
I have looked at InAppSettingsKit but this also uses the standard settings bundles. Is it possible to use InAppSettingsKit to import settings updates dynamically from a remote list.
Are there other ways to do what I am trying to do?
You won't be able to change the list dynamically with regards to Settings.app. Settings.app always uses the static schema plist from your app bundle. (You could resort to a freeform text field but that probably doesn't catch your case.)
With InAppSettingsKit, you can accomplish that but you have to do some extra work: For the dynamic parts, you'll want to use a custom view controller, e.g. a table view controller.
Best way for you handle this is storing the multi-values in the NSUserDefaults and start using them. In this method, you will be able to update the values as well as it will be persistent across multiple sessions.
Edit (answers comment):
Saving it to user defaults.
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
#"your Object", "Your Key"
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
Retriving it:
[[NSUserDefaults standardUserDefaults] integerForKey:#"Your Key"];

Resources