0

I'm building my first real iOS application. In my app I'm using REFrostedViewController (https://github.com/romaonthego/REFrostedViewController), which uses three viewcontrollers to construct a slideout menu; a rootviewcontroller, a homeviewcontroller and a rootviewcontroller.

Before the rootviewcontroller, I've created a login screen. If the login is successful, a token is send back from the server. This token is needed for further requests to the server. The app then performs a segue called login_success. Now, normally I'd send the token to the rootviewcontroller with something along the lines of this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"login_success"]) {
        RootViewController *controller = (RootViewController *)segue.destinationViewController;
        controller.xAuthToken = _xAuthToken;
    }
}

However, I can't use this to send it to multiple viewcontrollers. How can I do that?

user4191537
  • 203
  • 2
  • 14
  • May be Notifications. – Anoop Vaidya Nov 21 '14 at 13:06
  • I would have an object responsible for all network call, that has the token property, and then you can pass the network call object or use it has a singleton. Another possibility is to save the token in keychain or userdefault depending on the criticity of the token – Boris Charpentier Nov 21 '14 at 13:18

2 Answers2

1

For storing your authentication token it's preferable to use NSUserDefaults

Here is an example how you can use it : Example

However NSUserDefaults is not secure or encrypted, so if you want a secure alternative I would highly recommend you use the keychain - it's exactly what Facebook do for storing their session tokens.

Apple have some sample code GenericKeychain that shows a basic implementation

0

There are many ways to achieve this you have to choose the better one according to your needs

Approach 1

You have to set root view controller or any other view controller as responsible view controller and then you suppose to pass to your object to responsible view controller from that you can pass it to other view controllers whenever it requires.

Approach 2

You can use NSNotificationCenter which provides a mechanism for broadcasting information within a program. look at the below link which will help you to implement.

NSNotificationCenter example

Approach 3

You can store it in NSUserDefaults and also you can retrieve it back wherever you requires look at the below link to implement this

NSUserDefaults example

Community
  • 1
  • 1
thavasidurai
  • 1,972
  • 1
  • 26
  • 51