AFNetworking and Cookies

后端 未结 2 1824
孤独总比滥情好
孤独总比滥情好 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:33

    You do not need to bother with NSUserDefaults nor any keychain wrapper if you use NSURLCredential. Indeed NSURLCredential is much simpler to use, as it allows you to store both username and password in the keychain in two lines of code.

    Your code would be something like that once the user is logged in:

    NSURLCredential *credential;
    
    credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
    [[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.loginProtectionSpace];
    

    Then, each time the app is launched, you can check whether your user was already logged in by searching for any credential in order to automatically log back your user (if need be):

    NSURLCredential *credential;
    NSDictionary *credentials;
    
    credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.loginProtectionSpace];
    credential = [credentials.objectEnumerator nextObject];
    NSLog(@"User %@ already connected with password %@", credential.user, credential.password);
    

    You also need to clean the credential when the user wants to log out:

    NSURLCredential *credential;
    NSDictionary *credentials;
    
    credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.loginProtectionSpace];
    credential = [credentials.objectEnumerator nextObject];
    [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:self.loginProtectionSpace];
    

    loginProtectionSpace is created once for all. Please, note this sample code assumes there is only one credential in this space, which is usually the case unless you manage several accounts.

    Here is an example of how you would create a NSURLProtectionSpace:

    NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
    self.loginProtectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host
                                                                      port:[url.port integerValue]
                                                                  protocol:url.scheme
                                                                     realm:nil
                                                      authenticationMethod:NSURLAuthenticationMethodHTTPDigest];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题