Skip to content

Instantly share code, notes, and snippets.

@johnciacia
Created April 1, 2012 21:20
Show Gist options
  • Save johnciacia/2278896 to your computer and use it in GitHub Desktop.
Save johnciacia/2278896 to your computer and use it in GitHub Desktop.
Send POST Request
The interface expects one UIButton and two UITextFields. The UIButton is connected to the `loginButtonClicked` IBAction and each text field is connected to the respective IBOutlet.
<?php
echo $_POST['username'] . ":" . $_POST['password'];
//
// ViewController.h
// LoginDemo
//
// Created by John Ciacia on 4/1/12.
// Copyright (c) 2012 Compiled Thoughts. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UITextField *usernameTextField;
IBOutlet UITextField *passwordTextField;
}
- (IBAction)loginButtonClicked:(id)sender;
- (void)doLogin:(NSString *)username:(NSString *)password;
@property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
@property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
@end
//
// ViewController.m
// LoginDemo
//
// Created by John Ciacia on 4/1/12.
// Copyright (c) 2012 Compiled Thoughts. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize usernameTextField;
@synthesize passwordTextField;
/**
* Send a POST request with a 'username' and 'password' to http://www.example.com/index.php
*/
- (void)doLogin:(NSString *)username:(NSString *)password {
NSString *url = [NSString stringWithFormat:@"http://www.example.com/index.php"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
//create the body
NSString *post = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];
NSData *postBody = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//set headers
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];
//set request body
[request setHTTPBody:postBody];
//get response
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if ([urlResponse statusCode] == 200 ) {
NSLog(@"Response: %@", result);
} else {
NSLog(@"There was an error. Return code: %d", (int)[urlResponse statusCode]);
}
}
- (IBAction)loginButtonClicked:(id)sender {
[self doLogin:usernameTextField.text:passwordTextField.text];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment