What is the correct way to display a loading indicator when getting data from a url

I'm a bit new to objective-c, I am developing an iOS news app, the app gets all its content using JSON parsing from url, I use AFNetworking

to do this and this is the method I did:

- (void)getContents
{
    NSString *urlString = @"http://some-url-that-has-json-output/";
    urlString = [urlString stringByAppendingString:self.articleId];
    NSLog(@"The call url is: %@",urlString);
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //AFNetworking asynchronous url request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"The JSON data is: %@", responseObject);
        jsonContents = [responseObject objectForKey:@"article"];
        [self LoadStructure];
    } failure:nil];
    [operation start];
}

      

Now the data is loaded with this method.

My question is: How to display a loading indicator (maybe a GIF) when data is received? and is this method above correct or better way to get data from url?

+3


source to share


2 answers


You can use the default iOS loading indicator (UIActivityIndicator). You must start the animation before the completion block and must hide the inner success and failure block.

You should create a method that uses the indicator as a class variable:

indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.hidesWhenStopped = YES;
indicator.frame = CGRectMake(35, 15, 30, 30);
[self.view addSubview:indicator];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                     initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[indicator startAnimating];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"The JSON data is: %@", responseObject);

      



//stop:

[indicator stopAnimating];
    jsonContents = [responseObject objectForKey:@"article"];
    [self LoadStructure];
} failure::^(AFHTTPRequestOperation *operation, NSError *error){
    [indicator stopAnimating];
}];
[operation start];

      

+1


source


Drag UIActivityIndicatorView

an XIB into your view and connect it to the IBOutlet.

.h file

@property(nonatomic, strong)IBOutlet UIActivityIndicatorView * activityIndicator;

      

Add a UIActivity indicator view to your view. show it before



NSString *urlString = @"http://some-url-that-has-json-output/";

using: [self.activityIndicator startAnimating];

and stop it inside completion block and bounce block

using: [self.activityIndicator stopAnimating];

+1


source







All Articles