r/iosdev • • 3d ago

Is HTTP loopback OAuth actually feasible in a native iOS app? (Sign in with ChatGPT / SwiftUI)

/r/iOSProgramming/comments/1y7vjqq/is_http_loopback_oauth_actually_feasible_in_a/
1 Upvotes

1 comment sorted by

2

u/kokerali 2d ago

Your suspicion is very likely right. A few seconds after Safari comes to the front your app is suspended, and a suspended app can't accept connections, so the redirect to 127.0.0.1 has nothing on the other end. That would also explain the spinner. beginBackgroundTask buys a short and not guaranteed window, too fragile for a consent screen where people may need to log in first.

A quick way to confirm: log something in the NWListener newConnectionHandler. If nothing arrives while Safari is in front, it's suspension.

Keeping the auth page inside your app keeps the app active, and then the redirect is just a page load your listener can answer:

  • SFSafariViewController: open the authorize URL, answer the callback with a small "you can close this" page, then dismiss it yourself once you have the code.
  • ASWebAuthenticationSession: give it a callback scheme that never matches, and call cancel() once the listener has the code. A bit awkward, but the redirect URI stays exactly as the provider requires.

Either way, bind the listener to loopback only so it isn't reachable from the network, and check the state value before exchanging the code.

Worth knowing too: RFC 8252 describes loopback redirects mainly for desktop apps and recommends a custom scheme or claimed https redirect on mobile. If the docs only mention loopback, that flow may have been written with desktop and CLI clients in mind, so asking whether a mobile redirect option exists could save you the workaround.