views:

220

answers:

2

Hello.

I have two question:

I'm developing an iPhone application and I want to know when it is the first time the application is executed. I want to check some extended permissions from facebook the first time.

  1. How can I know that? (first question)

Another way to solved this problem is to store the extended permissions granted in some configuration file. I don't want to make visible this file through app settings icon.

  1. How can I add some configuration files to store these permissions granted? (second question)

Thanks

+2  A: 
  1. The std way would be to check if there is a value in NSUserDefaults and if that value doesn't exist (such as on first run) then create the value so on the rest of the starts you will know its not first run.

  2. This is also a good candidate to use the NSUserDefaults and set key/value pares for each setting/permission you want.

Here's a quick tutorial on using NSUserDefaults.

Summary from that site for Saving:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

Loading:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

NSUserDefaults is perfect for saving app settings, and purchase history but if you are wanting to store much data then you should use another way.

jamone
If I use NSUserDefaults, can the user change these values outside the application? (I mean, editing these values throw the settings icon on iPhone).
VansFannel
No, only your app can change them. Unless they have jail broken their device, then the only real safe place is the keychain, but even that may not be secure. All bets are off on jail broken hardware.
jamone
A: 

Just write to a local file (xml or whatever). Check, first, for the existence of the file. If not there this is the first run - otherwise just read the file.

Phil Nash