r/iOSProgramming • u/Nian99 • 2h ago
Question Is HTTP loopback OAuth actually feasible in a native iOS app? (Sign in with ChatGPT / SwiftUI)
I’m developing a native SwiftUI iOS app and trying to implement OpenAI’s Sign in with ChatGPT (SIWC) open-source plan-usage flow.
The documented flow requires:
- A local HTTP callback at http://127.0.0.1:<port>/auth/callback
- A local listener running before authorization starts
- Opening the system browser
- Receiving the authorization code through the loopback callback
My implementation uses Apple’s Network framework (NWListener) and opens the authorization page in external Safari.
On a physical iPhone, the OpenAI consent page loads and displays the correct account and subscription information. However, the consent button keeps spinning indefinitely, and the app never receives the callback.
My suspicion is that iOS suspends the app while external Safari is active, which prevents the local listener from receiving the redirect.
I’ve considered a few alternatives:
- SFSafariViewController to keep the app in the foreground.
- ASWebAuthenticationSession, although its callback handling does not appear to support HTTP loopback.
- An officially supported alternative OAuth flow for native iOS apps.
The problem is that OpenAI’s documented SIWC open-source flow explicitly requires HTTP loopback and a system browser.
My questions:
- Has anyone successfully implemented a local HTTP loopback listener in a native iOS app while completing OAuth in an external browser?
- Does iOS actually suspend NWListener when the app enters the background?
- Could SFSafariViewController keep the listener alive, and would this be a technically sound approach?
- Is there another standards-compliant approach that would work without changing the provider’s required redirect URI?
I’m specifically looking for a solution that does not involve API keys, cookie extraction, password handling, proxies, or undocumented endpoints.
Any practical experience or technical insight would be greatly appreciated.
1
u/youthdancing 2h ago
I am interested in this as well. Did you try this yet? It should keep your app running in the background: https://developer.apple.com/documentation/backgroundtasks/performing-long-running-tasks-on-ios-and-ipados
1
u/Green_Start2329 1h ago
Use https://developer.apple.com/documentation/authenticationservices (ASWebAuthenticationSession), that way the entire web authorization flow stays within your app, keeping the loopback listener alive.
"SFSafariViewController" won't work because it doesn't support things like Passkey authentication.
1
u/Doctor_Fegg 1h ago
ASWebAuthenticationSession plus a local webserver (using whatever the modern equivalent of GCDWebServer is) should work, shouldn’t it?
•
u/PiXeL161616 45m ago
Apple DTS covers the listener part here: a TCP connection can succeed while the app is suspended, but the app can't handle it, so the client hangs. Backgrounded isn't necessarily suspended. That makes suspension worth testing, but it doesn't establish why this consent page spins or whether
BGContinuedProcessingTaskfixes it.1
u/[deleted] 2h ago
[removed] — view removed comment