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.