0
全部,NetworkManager for NSURLConnection
我已經創建了幾個ViewControllers的應用程序。在viewController中,我使用NSURLConnection從Web服務器獲取數據。現在,在每一個視圖控制器,我與發佈數據:
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];
,並在每一個的viewController我有以下幾種方法:
- - (無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據{
- - (無效)連接:(NSURLConnection的*)連接didFailWithError:(NSError *)錯誤{
- - (無效)connectionDidFinishLoading:(NSURLConnection的*)連接{
這一切都是多餘的,因爲在這些方法中的代碼是在每一個的viewController相同。
所以我想創建它在每一個的viewController用來做所有的網絡(發送後和處理respons)模型類「的ConnectionManager」。
問題,我有:
- 是否有一個教程或示例代碼如何建立呢?
- 我該如何才能在connectionManager中捕獲到響應(所以「連接」方法)
- 如何通知正確的viewController接收到數據並且ViewController必須對數據做些什麼?