iOS8 Weirdness Part4: URLSessionTask
This post is one of those short ones. Again I’m confused how switching from one iOS to another may transform your day to a horror. So what is this all about, if you are typical user of
NSURLConnection you should acutally know that Apple introduces
NSURLSession as a new way to handle networking. And if somethings goes wrong you need to handle it somehow…
And this is where all things starting to be tricky; typically in iOS7 when you get an error from your request you handle it in some fail block passing
NSError so my today talk is actually about
NSError. Sometimes you need to handle paritcular error in way that is more readable for your user eg.: connecting to VPN, Timeouting etc. So let’s say the error that is contained in
NSError class need to be modified to something more readable eg by using CODE and DOMAIN to specify what we should actually show to the user. But in most of cases NSError contains quiet good description and it’s even localized! But not in iOS8…
Some time ago i saw empty
UIAlertView in some of the app i was working on. So i start digging what acutally happens, few minutes with debugger show me that
NSError if something fails but it do not contains
NSLocalizedDescriptionKey in userInfo the key is acutally missing! And using localizedDescription property also retuns some very unfriendly messages. So lets see what you will see in debugger:
1 2 3 4 5 6 7
As you can see we can find
NSLocalizedDescription ther. Now let’s try to run same code on iOS8:
1 2 3 4 5 6 7 8
As i said no
NSLocalizedDescription at all, the worst thing is that no matter error your connection will generate, it will be always called the same “The operation couldn’t be completed.” So if you have some production build and you try to get some info from your user, you are in serious trouble. The only way to fix this for now is to build some extension that will convert CODE and DOMAIN to apropriate error message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
And how to use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Of course you may need to handle way more cases here so it is good to check all code list in documentation. In this example im using localization to support multiple languages by myself if you do not need to do that just simply type there your description.
And that’a all for today, next time some swift cool features.