0

I'm developing an application, and I'm getting the data of JSON from the Database using MAMP PRO for now. This is the Code:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.8/ArabLineDatabase/GetSocialNetworkCount.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

 NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.8/ArabLineDatabase/GetSocialNetworkCount.php"]];

  JsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (connection)
    {
        NSLog(@"Connecting.....GetSocialNetworkCount.M");
    }
    else
    {
        NSLog(@"Can't connect.....GetSocialNetworkCount.M");
    }

The problem is whenever there is no internet or i close the server so it will not be reachable my application crash, and i get an error that the JSON Data is nil How i can prevent that?

Ahmed jamal
  • 73
  • 1
  • 1
  • 5

1 Answers1

0

You basically need to cancel your request after a certain time interval, in case the server isn't responding or you don't have a network or something similar.

[connection cancel]; 

This will solve your problem

Community
  • 1
  • 1
Umar Farooque
  • 2,049
  • 21
  • 32