Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created October 20, 2012 01:44
Show Gist options
  • Save ChrisRisner/3921647 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3921647 to your computer and use it in GitHub Desktop.
iOS Day 8
- (IBAction)btnFetchData1:(id)sender {
NSString *queryString = [NSString stringWithFormat:@"http://chrisrisner.com/Labs/day7test.php?name=%@", [self.txtName text]];
NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:
queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (con) {
_receivedData=[NSMutableData data];
} else {
//something bad happened
}
}
[theRequest setHTTPMethod:@"POST"];
NSString *post = [NSString stringWithFormat:@"postedValue=%@", [self.txtName text]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
[theRequest setHTTPBody:postData];
NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:
queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSDictionary* jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"Value1", @"Key1",
@"Value2", @"Key2",
nil];
NSError *error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary
options:NSJSONWritingPrettyPrinted error:&error];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// should check for and handle errors here but we aren't
[theRequest setHTTPBody:jsonData];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment