3

I am a .Net developer and very new to Xcode. I am using WCF for the server side process and send/receive JSON data between iphone app and WCF. I want the native iPhone app to do below:

  1. Registration and Login view
  2. Once user click login button, it sends username and password entered in JSON format to my WCF service to authenticate, if login is valid, WCF will return a valid member GUID and a token string
  3. iPhone app receives the member guid and token, and stores them in a "persistent variable", and I will load new views for authenticated users. So member has access to all functions/views.
  4. Every time I post data between iphone app and WCF, I need to use that member guid and token from the variable so the WCF service knows which member is requesting data.
  5. Every time user shutdown or restart iphone, I hope my app can remember the member guid, so they don't have to login again.
  6. User can click a logout button, and it will clear the variable and return to login view.

I am not sure if this is the correct way of iphone app login process?

I have already done WCF authentication and send back JSON, but now I need to store member GUID, Can someone please point me to the right direction? What should I use to store that information that can be used later on? An example would be great!

Daniel
  • 23,129
  • 12
  • 109
  • 154
paul1155
  • 107
  • 1
  • 9

3 Answers3

5

Sounds like the perfect sort of thing for the NSUserDefaults. Use them like this:

NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];
[myUserDefaults setObject: @"the key" forKey: @"key"];

You can also use other functions to set different kinds of data, like setInteger:forKey: or setValue:forKey:. To recall your data, use this function:

NSString *theKey = [myUserDefaults objectForKey:@"key"];

and the return value will be the object you stored in it. Other data types work similarly with functions like integerForKey: and valueForKey:.

WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • This is exactly what I need for my app. Unfortunately, I went in and created a custom solution using core data. TODO: migrate existing code to use NSUserDefaults. – Brian Aug 12 '12 at 01:18
4

You should know that you must respect the sandbox of your application, this means, you can't store something "permanently" on the device.

Usually this kind of logic (saving tokens etc) is done by saving that data into the NSUserDefaults.

The NSUserDefaults is a Property List of the application and you can store data as a key-value couple.

You can save something for example by doing the following:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:@"mylongtoken" forKey:@"com.myapp.apitoken"];
[ud synchronize];

If you delete the app this data will also disapear. This data is persisted when the app is updated via the App Store and it will be backed up to iTunes/iCloud as part of the device backup.

You can also synchronise this property list with iCloud but for that pease check out this post: Can I use iCloud to sync the NSUserDefaults plist file

Community
  • 1
  • 1
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • thank you for your reply Daniel. By permanent I mean to maintain that piece of data when users shutdown or restart their device, so they don't need to login again next time they use the app. If user choose remove the app, that data should be removed completely I agree. – paul1155 Aug 12 '12 at 02:16
1

Actually, if you're storing credentials, you should probably put that into a keychain. NSUserDefaults can be loaded off a device rather easily and is saved in the clear.

Here's a nice keychain utility library that should be helpful: https://github.com/ldandersen/STUtils/tree/master/Security

You can save credentials like so:

[STKeychain storeUsername:@"username" andPassword:@"password" forServiceName:@"namespace" updateExisting:YES error:&error];

And then retrieve them like so:

NSString *password = [STKeychain getPasswordForUsername:@"username" andServiceName:@"namespace" error:&error];
ralphshao
  • 21
  • 2