How to remain logged in until user decides to logo

2019-03-04 03:45发布

问题:

I am designing an app that it uses the web service like posting links and pictures. The first view controller checks the default username and password of the user and if it is correct, it allows the user to login to the home view controller but if its not correct it directs the user to the login view controller (if its the first time that the application is running it also directs the user to the login view controller).

I have different view controllers that is connecting to the home view controller. For example one of them is for posting pictures to the website, the other one is for posting links to the web and the other view controller is for changing the preferences of the users profile.

I am using RestKit API to POST on web and here is the code that I use for posting:

- (IBAction)addLinkPressed:(UIButton *)sender {

        [RKClient clientWithBaseURLString:@"http://MyWebsite.com"];

        NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                self.linkField.text, @"url",
                                self.linkTitleField.text, @"title",
                                self.linkSummaryField.text, @"summary",
                                nil];

        RKRequest *request = [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];
        [request setUserData:@"sendLink"];   
}

For each view controller I put the following method in viewDidLoad in order to get authentication for posting :

- (void)autoLogin {

    [RKClient clientWithBaseURLString:@"http://MyWebsite.com"];    
    [RKObjectManager sharedManager].client=[RKClient sharedClient];
    RKParams *parameters = [RKParams params];

    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] forParam:@"username"];
    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] forParam:@"password"];

    [[RKClient sharedClient] setAuthenticationType:RKRequestAuthenticationTypeHTTP];

    RKRequest *request = [[RKClient sharedClient] post:@"/login.php" params:parameters delegate:self];
    [request setUserData:@"login"];
}

Question: Is there any method to get authentication from the website for posting other than logging in for each view controller? the problem for logging in for each view controller is that some times logging process gives an error and does not allow the user to POST. I need a method that as soon as the user inputs the correct username and password, It remains logged in until the user logout by himself or delete the app.

I found this relative question but It was not that helpful or I did not understand the answers well. Any thoughts or ideas would be appreciated.

回答1:

RestKit is designed to consume APIs. Usually APIs are stateless, meaning that there is no such thing as state between requests. On traditional websites state is achieved by transmitting cookies back and forth between client and server. Apparently RestKit does not handle cookies (which is good in my opinion). From the top of my head I can think of two solutions:

  • Apparently RestKit does not handle cookies automatically. Thus, you could get the cookie after the login and attach it to every subsequent request.
  • Better: use OAuth2, which is basically a token that the user receives when logging in. This token is then appended as a parameter to each request.

Both methods are basically the same. You have to send a token that identifies the user as logged in.



回答2:

If I understand well your situation, you have many viewcontrollers that act separately and independently. Doing so you need as many logins as the number of viewcontrollers your app manages.

If this is your scenario, you can try to create a single class that does the login work and use your other viewcontrollers just to handle GUI.

This will be your architecture:

App other classes and viewcontrollers -> LOGINMANAGER -> viewcontroller 1 (pictures), viewcontroller 2 (links), viewcontroller 3 (preferences), viewcontroller 4 (whatever)...