Hamza Imran Hamza Imran - 1 year ago 55
iOS Question

Activity Indicator on a button click in IOS

I have a button of submitting form data to the service. When user click it it submit data and give alert of success. I want to add activity indicator in it , when user click button of submit the time it takes to submit the data the activity indicator should move and than hide when it data is submitted.I'm confused where should i use it in the code , I'm confused at that whether it shall be call after response or anywhere else. My code is,

NSString *post = [NSString stringWithFormat:@"propertyfor=%@&propertytype=%@&landarea=%@&country=%@&city=%@&price=%@&description=%@&location=%@&name=%@&email=%@&phone=%@",_propertyfor,_propertytype,_landarea,_country,_city,_price,_propdes,_propdescrip,self.name.text,self.email.text,phone];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"My URL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}] resume];

Answer Source
     **//Start Activity Indicator here.**
    NSString *post = [NSString stringWithFormat:@"propertyfor=%@&propertytype=%@&landarea=%@&country=%@&city=%@&price=%@&description=%@&location=%@&name=%@&email=%@&phone=%@",_propertyfor,_propertytype,_landarea,_country,_city,_price,_propdes,_propdescrip,self.name.text,self.email.text,phone];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"My URL"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
     **//Stop Activity Indicator here.**
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"requestReply: %@", requestReply);
    }] resume];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download