0

I'm developing an iOS App in Xcode and using AFNetworking to talk to the API - the API uses "X-Auth-Token". So when I log in and store the session token in the User Defaults, it works perfectly until the app is restarted - the session token doesn't work then for some reason?

The token is used in AFNetworking like so:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:@"SessionTokenFromUserDefaults" forKey:@"X-Auth-Token"];

Does anyone have any advice? (PS: the error I get from AFN when using the token after restarting is: Request failed: unauthorized (401)

How could I acheive this without logging in each session?

Thanks a lot!

Declan Land
  • 639
  • 2
  • 11
  • 27

3 Answers3

0

I remember having that sort of problems using NSURLConnections without a cache policy. I think there is a thread here that might help.

Community
  • 1
  • 1
kyzrfranz
  • 11
  • 1
0

It is very likely that the server report Request failed: unauthorized (401) is because the token is empty.

To debug, You can add these two lines in your AppDelegate's

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[AFNetworkActivityLogger sharedLogger] startLogging];
    [[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelDebug];
}

Then you can check the request in your log to see if the token has actually been set in the header.

Jakehao
  • 1,019
  • 8
  • 15
0

I found the solution to my problem - here is what I used:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:sessionToken forHTTPHeaderField:@"X-Auth-Token"];
Declan Land
  • 639
  • 2
  • 11
  • 27