iOS App Authentication Without UIWebView
Yes, you most certainly can do this. This is how I typically do it, change it to your needs (keep in mind that NSJSONSerialization is only available in iOS 5+):
@property (nonatomic) NSOperationQueue *basicQueue; //add this to your header or as an instance variable-(void)logMeIn:(NSString *)username password:(NSString *)password {basicQueue = [[NSOperationQueue alloc] init];NSDictionary *userCredentialsDict = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username", password, @"password", nil];NSData *encodedDict = [NSJSONSerialization dataWithJSONObject:userCredentialsDict options:NSJSONWritingPrettyPrinted error:NULL];NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://yoursite.com/login"]];[request setHTTPMethod:@"POST"];[request setHTTPBody:encodedDict];[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];[NSURLConnection sendAsynchronousRequest:request queue:basicQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; if (httpResponse.statusCode == 200) { //do stuff NSLog(@"Received data, parsed into string: %@", stringFromData); } else { //do error stuff or whatever }}];
}
For more information, check out
- https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
- https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html
- http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html