Search code examples
iosjsonnsurlconnection

How to perform multiple JSON connections in the same viewController


In a viewController I am using the following code to execute a JSON connection:

In viewDidLoad method:

 //URL definition where php file is hosted
    NSURL *url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
    // URL request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //URL connection to the internet
    [[NSURLConnection alloc]initWithRequest:request delegate:self];


//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    [data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    //array waterfalls populated via JSON from database
    categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

    NSLog(@"NUMERO DE EMPRESAS = %lu"
          , (unsigned long)[categorias count]);
}

I need to do the same in the same viewController but requesting two other different PHP files to create two more NSArrays.

I don't find problems just making a copy of the code inside the viewDidLoad method, renaming NSURl / NSURLRequest and changing the URL for the two others URLs, but I don't know how to implement the connection methods for the two new URLs.


Solution

  • //PROCESSING FIRST CONNECTION
    NSURL *first_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
    NSURLRequest *first_connection_request = [NSURLRequest requestWithURL:first_connection_url];
    NSURLConnection *first_connection=[[NSURLConnection alloc]initWithRequest:first_connection_request delegate:self];
    
    //PROCESSING SECOND CONNECTION
    NSURL *second_connection_url = [NSURL URLWithString:@"http://url_of_second_string.php"];
    NSURLRequest *second_connection_request = [NSURLRequest requestWithURL:second_connection_url];
    NSURLConnection *second_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self];
    
    
    //methods to perform the connection and population of data
    
    -(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        if(connection==first_connection){
             data_for_first_connection = [[NSMutableData alloc]init];
        }
        else if(connection==second_connection){
             data_for_second_connection = [[NSMutableData alloc]init];
        }
    }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
    {
        if(connection==first_connection){
             [data_for_first_connection appendData:thedata];
        }
        else if(connection==second_connection){
             [data_for_second_connection appendData:thedata];
        }
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        //if data received network indicator not visible
        [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
    
        if(connection==first_connection) {
            //array waterfalls populated via JSON from database
        categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    
        NSLog(@"NUMERO DE EMPRESAS = %lu"
          , (unsigned long)[categorias count]);
        }
        else if(connection==second_connection){
            // PROCESS TO BE DONE FOR SECOND CONNECTION
        }
    }