Question

I am trying to send an asynchronous Post request to a php url, but the request requires a parameter of "Password" and a value of "EGOT". I am pretty sure I need to use this:

(void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

The only problem is that when I start to type it in my viewController implementation file, it does not get recognized. Do I need to import NSURL for this? I thought that UIVIewcontroller already inherits from the class. If this is not the proper method to get this request please explain to me how to get it, it'll be much appreciated.

Was it helpful?

Solution

You can create your request object like this (NSMutableURLRequest is a mutable subclass of 'NSURLRequest'):

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request addValue:@"EGOT" forHTTPHeaderField:@"Password"];

And then call NSURLConnection sendAsynchronousRequest:queue:completionHandler: with that request.

OTHER TIPS

Use below code for Asynchronous request with post method...

NSString *strupload=[NSString stringWithFormat:@"uid=%@&password=%@&oldpassword=%@",appdel.strUserid,txtConfirmPswd.text,txtOldPswd.text];
NSString *strurl=[NSString stringWithFormat:@"%@change_password.php?",LocalPath];
NSString *strpostlength=[NSString stringWithFormat:@"%d",[strupload length]];
NSMutableURLRequest *urlrequest=[[NSMutableURLRequest alloc]init];

[urlrequest setURL:[NSURL URLWithString:strurl]];
[urlrequest setHTTPMethod:@"POST"];
[urlrequest setValue:strpostlength forHTTPHeaderField:@"Content-Length"];
[urlrequest setHTTPBody:[strupload dataUsingEncoding:NSUTF8StringEncoding]];

[NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSError *error1;
     NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
 }];

You can use AFNetworking for upload and MBProgressHUD for progress display

AFHTTPClient *httpClient                    = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlLink]];
    NSMutableURLRequest *request;

request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                        path:nil
                                                  parameters:postDictionary
                                   constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                   }];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminate;
    hud.labelText = @"Uploading";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        float uploadPercentge           = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
        float uploadActualPercentage    = uploadPercentge * 100;
        hud.progress                    = uploadPercentge;
        if (uploadActualPercentage >= 100) {
            hud.mode        = MBProgressHUDModeText;
            hud.labelText   = [NSString stringWithFormat:@"Waiting for response"];
        }
    }];
    [httpClient enqueueHTTPRequestOperation:operation];

    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [hud hide:YES];
        NSLog(@"Success %@",  operation.responseString);
        NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",message);
    }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"error: %@",  operation.responseString);
                                          NSLog(@"%@",error);
                                      }];
    [operation start];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top