If you use NSURLConnection and the session is cookie-based, this will be done automatically. So, all you would need to write would be like this
NSMutableURLRequest *request = nil; request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://server.com/login.php"]]; NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", @"<username>", @"<password>"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; [request setTimeoutInterval: 15]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData]; _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [_urlConnection start];
And you will also have to implement NSURLConnectionDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
If you have other information in the headings that you need to send back with subsequent requests, you can read it from an answer like this
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields];
And set the header fields for the next request, such as
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
To save information , be it a username and / or password or session information, you can use NSUserDefaults
//To save NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; if (standardUserDefaults) { [standardUserDefaults setObject:@"<username>" forKey:@"username"]; [standardUserDefaults setObject:@"<pass>" forKey:@"password"]; [standardUserDefaults synchronize]; } //To retrieve NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; NSString *val = nil; if (standardUserDefaults) val = [standardUserDefaults objectForKey:@"username"];
Finally, it would be wise to build a model to map the XML API using [For example: user class with username and passwords].
Google for Apple docs in MVC.
Hope this helps!