Question

When I run my app and if there in't internet connection it terminate my app directly and display this message.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil

- (void)viewDidLoad
{
response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://example.com/jsonTest.php"]];

NSError *parseError = nil;

jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];

jsonArray1 = [[NSMutableArray alloc] init];

for(int i=0;i<[jsonArray count];i++)
{
   ...
   ..
   .
}

}

I have tried this code below to show an alert view but nothing to show, am I doing something wrong?

-(void)uploadRequestFailed:(NSJSONSerialization *)request
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No internet connection" message:@"Please check the internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
Was it helpful?

Solution 2

Put the condition like this

if(response!=nil) {

    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];

    jsonArray1 = [[NSMutableArray alloc] init];

    for(int i=0;i<[jsonArray count];i++)
    {
       ...
       ..
       .
    }
}

OTHER TIPS

If you have no internet connection, your response object will be nil. With so, your NSJSONSerialization call will raise an exception, since the first parameter is nil.

You should check if response is nil before assuming you can instantiate a JSON with it.

Its because you are doing webcall and getting data from server

response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://example.com/jsonTest.php"]];

but there is no active connection so you will get nothing in response variable. and when you pass that response to jsonarray can't parse it. so the error occurred.i would suggest that before doing webcall check for the internet connection. if there is active connection then only you can make call to server. you can check internet connection like this:

- (BOOL) connectedToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags)
    {
        return NO;
    }

    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable && !needsConnection) ? YES : NO;
}

for this code to work add library:

SystemConfiguration.framework

and import this to your class

#import <SystemConfiguration/SystemConfiguration.h>

Hope it Helps!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top