AFNetworking and Cookies

后端 未结 2 1837
孤独总比滥情好
孤独总比滥情好 2020-12-02 06:48

I\'m using AFNetworking as a network layer for my iPhone app which connects to a Rails server that uses Devise for authentication. If I sign in (with a POST call) providing

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 07:34

    Cookies are indeed automatically stored for the lifetime of your application for any subsequent requests on a particular server. A good strategy would be to store the username and password in the keychain or in NSUserDefaults like this:

    // Setting
    [[NSUserDefaults standardDefaults] setObject:username forKey:@"username"];
    [[NSUserDefaults standardDefaults] synchronize];
    
    // Getting
    NSString *username = [[NSUserDefaults standardDefaults] objectForKey:@"username"];
    

    You may want to use this in combination with AFHTTPClient to send your credentials along with every request in an Authorization HTTP header.

提交回复
热议问题