0

I'm trying to "refresh"/"update" a shared string in my app after a user has successfully authenticated. Here's what I have:

// MyAuth.h
//@interface
+ (NSString *)sharedAuthToken;

// MyAuth.m
+ (NSString *)sharedAuthToken {
    static NSString *_sharedAuthToken = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedAuthToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    });

    return _sharedAuthToken;
}

// AuthCtrl.m
- (void)saveOauthInfo:(NSString *)token {

    [MyAuth sharedAuthToken] = token; // getting error here
}

I would like to update or refresh the sharedAuthToken. How can I do this?

*Let me know if the code is too confusing or too cluttered..

goo
  • 2,230
  • 4
  • 32
  • 53
  • Do you want to just set the token in memory, or do you also want to store the new token in the user defaults? – rob mayoff Mar 17 '14 at 21:46
  • @robmayoff Thx for the quick reply. I would like to set the token in memory – goo Mar 17 '14 at 21:47
  • I don't want this to sound rude, but you're not going to do very well overall if you're piecing your app together by asking questions on Stack Overflow. You should find a good book or a series of online tutorials and really learn the tools you're using. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Mar 17 '14 at 21:55
  • @JoshCaswell Don't worry, no offense taken. Thank you. Looks like a good read, will check it out :) – goo Mar 17 '14 at 22:07

2 Answers2

2

Create another class method:

+(void) setSharedAuthToken:(NSString *) token;

Move _sharedAuthToken from the sharedAuthToken method to outside the scope of the class but still inside the MyAuth.m file:

static NSString *_sharedAuthToken;

@implementation MyAuth ...

Implement the new class method:

+(void) setSharedAuthToken:(NSString *) token {
   _sharedAuthToken = token; 
}

Then in your saveOauthInfo method:

[MyAuth setSharedAuthToken:token];
mharper
  • 3,212
  • 1
  • 23
  • 23
  • you're using an ivar in a class method? this won't compile; I think the solution was to save it to user defaults – Patrick Goley Mar 17 '14 at 22:18
  • That's a really good point. You could add a `static NSString *_sharedAuthToken` outside of the scope of the class implementation but inside of the .m file. – mharper Mar 17 '14 at 22:29
  • I'd consider wrapping the assignment in the setter with `@synchronized(_sharedAuthToken) { ... }` if you go this route. – greymouser Mar 17 '14 at 22:59
1

You have no setter method.

+ (NSString *)sharedAuthToken is a "getter", only returning data.

You should setup a @property, if it suits your needs, which would generate a setter for you, or create your own.

e.g.

+ (void)setSharedAuthToken:(NSString *)token { ... }

Of course, since you made your getter store the token in a static local variable and furthermore wrapped its setting in a dispatch_once to make it a singleton, you'll have to refactor all that.

greymouser
  • 3,133
  • 19
  • 22