- (IBAction)asynchronousConnection:(id)sender {
NSString *query = [NSString stringWithFormat:@"sample=%@", @"sample"];
NSData *queryData = [query dataUsingEncoding:NSUTF8StringEncoding];
NSString *url = @"http://localhost/sample/index.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:queryData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"receive response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSError *jsonError;
NSMutableArray *returnedData = [[NSMutableArray alloc] init];
returnedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
NSLog(@"%@", returnedData);
}
- (void)connectionDidFinishLoading: (NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"finish connection");
}
Saturday, April 6, 2013
xcodeで非同期通信
前回の投稿で非同期通信の仕方ができるようになりたいと書いたのですが、やってみたら意外とすぐできたので書いておきます
Labels:
objective-C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment