简体   繁体   中英

prevent app from crashing if there is no internet

My app is crashing when there is no internet. This is because

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
                                                               options:NSJSONReadingMutableContainers error:&error];

returns the error 'data parameter is nil' .

How can I prevent this from crashing as I am caching the jsonData using SDWebImage . So it should still be there even if there is no internet!

You can make sure that the code is executed just if the data is not nil:

if (jsonData) {
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
                                                               options:NSJSONReadingMutableContainers error:&error];
}

not handle only nil , and not proper format of json too and you can get the error msg for further

  @try {

        responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    }
    @catch (NSException *exception) {
        NSLog(@"exception %@",exception);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM