2012-03-06 164 views
9

我很難找到NSURLConnection委託方法實現的任何示例。NSURLConnection委託方法

我想通過點擊按鈕發送帶有HTTP帖子的數據。不知道如何製作「提交」屏幕和「提交」。 (我知道如何使用微調,並將使用它們)

我使用此代碼根據botton單擊操作,但無法使用任何委託的東西。不知道如何用我目前的設置來實現它們。

NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL: 
    [NSURL URLWithString:@"http://myURL.com"]]; 

    [request setHTTPMethod:@"POST"]; 

    NSString *postString = [wait stringByAppendingString:co]; 

    [request setValue:[NSString 
         stringWithFormat:@"%d", [postString length]] 
    forHTTPHeaderField:@"Content-length"]; 



    [request setHTTPBody:[postString 
          dataUsingEncoding:NSUTF8StringEncoding]]; 

    //[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 


    [SVProgressHUD dismissWithSuccess:@"Submission Successful"]; 

回答

8

您需要使用新的NSURLConnectionDataDelegate協議。

我發現了一些exemples這裏:

http://blog.kemalkocabiyik.com/index.php/2012/02/fetching-data-with-getpost-methods-by-using-nsurlconnection/

如果你可以閱讀葡萄牙語:在此代碼,您將使用GCD,活動指示燈http://www.quaddro.com.br/blog/desenvolvimento-ios/baixando-conteudo-com-nsurlconnection-e-nsurlconnectiondatadelegate-no-ios

+0

第二個鏈接,portugese一個真的很有幫助,即使我不明白protugese,謝謝.. – Bhimbim 2017-07-17 09:23:56

25
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Did Receive Response %@", response); 
    responseData = [[NSMutableData alloc]init]; 
} 
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    //NSLog(@"Did Receive Data %@", data); 
    [responseData appendData:data]; 
} 
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{ 
    NSLog(@"Did Fail"); 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Did Finish"); 
    // Do something with responseData 
} 
+0

不知道如何設置代理,因爲我有我所有的代碼後上方,在 - (IBAction爲)提交:(ID)發送當您啓動URL連接 – socbrian 2012-03-06 03:45:53

+0

所有四種方法將火。這應該在與啓動連接的方法相同的類中的實現文件中 – Eric 2012-03-06 03:50:40

+0

因此,你說這個代碼在我的提交類下?當我這樣做時,我收到錯誤。 – socbrian 2012-03-06 03:59:53

4
//Connection request 
-(void)requestURL:(NSString *)strURL 
    { 
     // Create the request. 
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]; 

     // Create url connection and fire request 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    } 


    //Delegate methods 
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     NSLog(@"Did Receive Response %@", response); 
     responseData = [[NSMutableData alloc]init]; 
    } 
    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
    { 
     //NSLog(@"Did Receive Data %@", data); 
     [responseData appendData:data]; 
    } 
    - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
    { 
     NSLog(@"Did Fail"); 
    } 
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSLog(@"Did Finish"); 
     // Do something with responseData 

     NSString *strData=[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 

     NSLog(@"Responce:%@",strData); 
    } 

http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

2

, UIButton操作 登錄按鈕 首先你會我在另一個線程上調用StartActivityindicator,它會一直移動,直到您移除或停止Activityindicator。 然後您將調用Web服務以在GCD隊列中登錄。 當您從服務器調用主隊列收到響應以更新UI時。

// After the interface declration 
@interface LoginViewController() 
{ 

NSData *responseData; 
dispatch_queue_t myqueue; 

}  
//Button Action 
- (IBAction)Login_Button_Action:(id)sender 

{ 

[NSThread detachNewThreadSelector: @selector(StartActivityindicator) toTarget:self withObject:nil]; 
myqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); 
dispatch_group_t group=dispatch_group_create(); 
dispatch_group_async(group, myqueue, ^{ [self loginWebService];}); 
} 
-(void)loginWebService 
{ 
//Combine Both url and parameters 
    NSString *UrlWithParameters = [NSString stringWithFormat:@"http://www.xxxxx.com?count=%@&user=%@&email=%@&password=%@",@"4",@"Username",[email protected]"UserEmail",@"PAssword String"]; 
//Pass UrlWithParameters to NSURL 
NSURL *ServiceURL =[NSURL URLWithString:UrlWithParameters]; 

NSMutableURLRequest *serviceRequest =[NSMutableURLRequest requestWithURL:ServiceURL]; 
[serviceRequest setHTTPMethod:@"POST"]; 

[serviceRequest setValue:@"application/json" forHTTPHeaderField:@"accept"]; 
[serviceRequest setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 

//GEt Response Here 
NSError *err; 
NSURLResponse *response; 
responseData = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&err]; 

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
NSInteger code = [httpResponse statusCode]; 
// check status code for response from server and do RND for code if you recive anything than 200 
NSLog(@"~~~~~ Status code: %ld",(long)code); 
if (code ==200) 
{ 
// your response is here if you call right 
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

dispatch_async(dispatch_get_main_queue(),^{ 
     // place the code here to update UI with your received response 
     [NSThread detachNewThreadSelector: @selector(StopActivityindicator) toTarget:self withObject:nil]; 
     }); 
} 
} 
//Activity indicator Method to display 
- (void) StartActivityindicator 
{ 
mySpinner.hidden = NO; 
[mySpinner startAnimating]; 
} 
- (void) StopActivityindicator 
{ 
mySpinner.hidden = YES; 
[mySpinner stopAnimating]; 
} 
+0

請加上這段代碼的簡短描述。也請花點時間回顧[回答] – 2015-09-03 11:55:19