ASWebAuthenticationSession Initialization Bug

1 minute read

I’ve recently been working on some enhancements to my latest app OctoNote that required me to do some refactoring in my network classes. I’m trying to logically group my network calls which meant moving a lot of messy stuff that was tossed in my View Controllers out into their own modules.

One of the things I recently moved in this refactoring re-introduced a bug I overcame a few months back with ASWebAuthenticationSession. The bug is essentially that the ASWebAuthenticationSession completion handler was never being called, causing the OAuth Alert to pop up on the screen and then immediately dismiss.

After searching the web and exhausting Stack Overflow trying to find a solution - including stumbling over my own article about AuthenticationServices - to no avail, I eventually just lucked into the same fix I discovered the first time.

When initializing and using a ASWebAuthenticationSession, you need to declare it as a class variable. Declaring it when you initialize the session and calling .start() causes this buggy behavior. I think it has something to do with how the alert gets displayed by the ASWebAuthenticationSession, but I’m not quite sure. I’ll update this post if I learn more.

The TL;DR for future me, don’t declare your ASWebAuthenticationSession variable in the function where you initialize it.