Search code examples
iosswiftcocoa-touchnsurlsessiondatatask

Does Apple test iOS apps offline? How to handle error if no connection?


I have submitted an app to iTunes and Apple rejected it because of a crash.

I symbolicated and analyzed the crashreport and saw that it crash at a json call.

I try to reproduce it and I found that it just happens when I turn off my wlan.

  1. Does Apple test apps offline?
  2. How can I handle this error? And make my jsoncall better.

This is my method:

    var session = NSURLSession.sharedSession();
    var uri = "/GetNews";
    let request : NSMutableURLRequest = CreateRequest(uri, HTTPmethod: "GET");

    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil;

        let jsonResult =  NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? Dictionary<String, AnyObject>;
        let resp : NewsResponse = NewsResponse(jsonData: jsonResult!);

        completionHandler?(resp);
    });
    task.resume();

It crashs at let resp..., because jsonResult is nil and I use !


Solution

  • Of course Apple tests apps offline, and you should too. You should plan on every call that requires an internet connection failing, and you should handle every error appropriately.

    The appropriately part is up to your app. For example, some apps (like Facebook) let you read posts you've already downloaded, and queue up posts you write to be sent when you get an internet connection. Some apps just don't work at all without an internet connection and it doesn't make sense for them to do anything but put up an error message (like, for example, a iTunes radio).

    If your app is a news reader, perhaps the best thing to do is use a cache and let them read news they've downloaded in the past. A simple, unobtrusive message letting them know they're offline and new articles will be downloaded once they're back on would suffice; a crash, though, is very bad in terms of usability and utility.