NSURLConnection委托方法

时间:2014-03-01 17:24:12

标签: ios nsurlconnection

我正在使用按钮操作来更新MySQL表字段的值。更新在Web服务器中执行,但我需要在视图控制器中更新UILabel文本。

这是我实施的代码:

- (IBAction)votarAction:(id)sender {
    //URL definition where php file is hosted
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

    dispatch_async(backgroundQueue, ^{

        int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

        NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
        NSLog(@"ID EMPRESA %@",string);
        NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
        [ms appendString:string];
        // URL request
        NSLog(@"URL = %@",ms);
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
        //URL connection to the internet
        NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];



        dispatch_async(dispatch_get_main_queue(), ^{
            //update your label


        });
    });
}
#pragma NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //buffer is the object Of NSMutableData and it is global,so declare it in .h file
    buffer = [NSMutableData data];
    NSLog(@"ESTOY EN didReceiveResponse*********");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
     NSLog(@"ESTOY EN didReceiveDATA*********");
    [buffer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Here You will get the whole data
     NSLog(@"ESTOY EN didFINISHLOADING*********");
    NSError *jsonParsingError = nil;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&jsonParsingError];
    //And you can used this array
    NSLog(@"ARRAY = %@",array);

    //HERE LABEL.TEXT UPDATE CODE  

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR de PARSING");
     NSLog(@"ESTOY EN didFAILWITHERROR*********");
}

正如我告诉你的那样,每次点击按钮时都会更新MySQL表中的字段值,但问题是从不调用NSURLConnection委托方法。 欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

在视图控制器的头文件中添加:<NSURLConnectionDelegate>

此外,没有必要将NSURLConnection投入单独的后台进程,也许这就是为什么不调用委托的原因。 NSURLConnection已经异步

也许尝试这样的事情:

- (IBAction)votarAction:(id)sender
{
    int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

    NSString *originalString = [NSString stringWithFormat:@"%d", categoriaID];

    NSMutableString *mutablesString = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
    [mutableString appendString:originalString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:mutableString]];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
    request.timeoutInterval = 5.0;

    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (data)
         {
             NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

             dispatch_async(dispatch_get_main_queue(), ^
             {
                 // Update your label
                 self.label.text = [array objectAtIndex:someIndex];
             });
         }
         else
         {
             // Tell user there's no internet or data failed
         }
     }];
}