Pregunta

Hi I'm using this code to send post values to a server but I want the HUD to appear during the time the request is being done, because it appears only when it ends the request.

-(IBAction)sendk:(id)sender {
/*HUD*/

        SLHUD *hudView = [SLHUD Mostrar:self.view]; // Creates a Hud object.
        hudView.text = @"Please Wait"; // Sets the text of the Hud.
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activityIndicator.alpha = 1.0;
        activityIndicator.center = CGPointMake(160, 280);
        activityIndicator.hidesWhenStopped = NO;
        [activityIndicator setTag:899];
        [self.view addSubview:activityIndicator];
        [activityIndicator startAnimating];
        /*FIN HUD*/

        NSString *post =[[NSString alloc] initWithFormat:@"user=%@&pass=%@",[username text],[password text]];

        NSLog(@"%@",post);
        NSURL *url=[NSURL URLWithString:@"URL TO SERVER"];

        NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"%ld",(long)[response statusCode]);

        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",responseData);
¿Fue útil?

Solución

The problem is that the code is blocking the main thread until the network request finishes. The screen will only update after the sendk method returns, but the method won't return until the sendSynchronousRequest method is finished. The solution is to dispatch the networking code (everything after /*FIN HUD*/) onto a background thread, or use sendAsynchronousRequest, and use the completion block to notify the main thread when the response arrives.

The code framework for using a background thread looks like this

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

    // do networking stuff here

    dispatch_async( dispatch_get_main_queue(), ^{

        // turn off the HUD and remove the spinner here
        // also do something with the network response here

    });

});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top