0
votes

I need to determine no internet exception when connecting to server without network. But on iOS I get IOException(On Android exception is ConnectException).

The message of exception contains this error message:

Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x10bc06330 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://..., NSErrorFailingURLKey=http://..., _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.}`

I can parse this message and check if it contains Code=-1009. But, is there a more elegant way to check NSError from JavaLangException?

1

1 Answers

0
votes

Unfortunately, there currently isn't a better way. We tried extending changing java.lang.Throwable's superclass to NSException, but that caused more problems than it solved. It also doesn't make sense to define new exception classes for each iOS networking error code, as that would increase the size of each app that uses j2objc. Consider using something like: if (exception.getMessage().contains("NSURLErrorDomain Code=-1009")) ... or define a isConnectionOffline(Exception e) method that does this check as well as one for Android.

You could file an enhancement request for some sort of NSURLErrorDomain exception wrapper, which could make these errors easier to decipher. Better yet, we love contributions if you come up with something useful. ;-)