All,
I have created an app with several ViewControllers. In the viewController, I get data from a webserver using NSURLConnection. Now, in every ViewController, I post the data with:
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.data.com/GetAllRequestsToPlay.php"]]];
NSString *post = [NSString stringWithFormat:@"PI=%i",self.ownPlayer.playerID.intValue];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPShouldHandleCookies:YES];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//set post data of request
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
and in every viewController I have the following methods:
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
this is all redundant because the code in these methods is in every viewController the same.
So I want to create a model class "connectionManager" which is used in every viewController to do all the networking (sending the post and handle respons).
Questions I have:
- Is there a tutorial or example code how to create this?
- how do I make it possible that the respons is caught in the connectionManager (so the "connection" methods)
- how can I notify the correct viewController that data is received and the the ViewController must do something with the data?