Login / Logout on an iPhone app

2020-05-24 12:04发布

问题:

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 app serves and the list of users it's on a website running php+mysql. What's the "standard" procedure for this? Is there any library for handling user's login on a remote site?

What solutions did you use? cookies? php sessions?

Any help or link to useful website will be much appreciated.

回答1:

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