简体   繁体   English

如果没有互联网,防止应用崩溃

[英]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' . 返回错误'data parameter is nil'

How can I prevent this from crashing as I am caching the jsonData using SDWebImage . 当我使用SDWebImage缓存jsonData时,如何防止崩溃。 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: 您可以确保仅在数据不为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 不仅不能处理nil,而且不能正确处理json的格式,因此您可能会得到错误消息msg

  @try {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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