0

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 !

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
BHuelse
  • 2,889
  • 3
  • 30
  • 41

2 Answers2

5

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.

Travis
  • 3,373
  • 20
  • 19
  • Thanks, I agree with you in all points. Next app I will test offline, too. For now I have to implement Justin Rose answer to some methods. thanks for this, too – BHuelse Jun 25 '15 at 15:54
3

TO FIX CRASH

As Eric stated, you should use "safe unwrapping", better known as optional binding. Try this:

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>;
  //if non-nil, assigns jsonResult to nonNilJsonResult
  if let nonNilJsonResult = jsonResult {
    let resp : NewsResponse = NewsResponse(jsonData: nonNilJsonResult!);
    completionHandler?(resp);
  } 
});
task.resume();
Justin Rose
  • 1,041
  • 7
  • 14
  • Yes, this will fix the crash, and I would also suggest passing a custom `NSError` through `completionHandler` so the lack of `jsonResult` can be handled appropriately. – jperl Jun 25 '15 at 15:17