Login / Logout on an iPhone app

后端 未结 1 1207
南旧
南旧 2021-02-04 22:10

I would like to know about managing user\'s login and logout on IPhone native apps. For example, every time my app is running, the user must be logged in. The information the ap

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 22:39

    Personally I get the user to enter their login info once, store it in a preference file then use that saved information when ever the server requests the user to authenticate - if your using NSURLConnection then you can use something like:

    -(void)connection:(NSURLConnection *)connection
    didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:[UserManager getUsername]
                                                 password:[UserManager getPassword]
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    
    } else {
    
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
    }
    }
    

    where [UserManager getUsername] and [UserManager getPassword] are class methods in a class that will load the username and password from a preference file

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