r/iOSProgramming • • 1h 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:

  1. SFSafariViewController to keep the app in the foreground.
  2. ASWebAuthenticationSession, although its callback handling does not appear to support HTTP loopback.
  3. 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 Upvotes

4 comments sorted by

•

u/youthdancing 40m 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

•

u/Nian99 8m ago

Thanks for the suggestion! I hadn’t looked into BGContinuedProcessingTask yet.

I’m building this as a native SwiftUI app and using NWListener for the local HTTP callback. The tricky part is that the OAuth flow requires opening the system browser, which sends the app into the background while the user signs in and approves access.

Do you know if BGContinuedProcessingTask can actually keep an incoming NWListener connection alive during that time? I understand it can keep certain work running in the background, but I’m not sure whether that also covers a local TCP listener waiting for an incoming callback.

Have you tried anything similar yourself? Would be really interesting to hear how it behaves on an actual device.

•

u/[deleted] 44m ago

[removed] — view removed comment

•

u/AutoModerator 44m ago

Hey /u/XenoCI_Hyunsoo, your content has been removed because Reddit has marked your account as having a low Contributor Quality Score. This may result from, but is not limited to, activities such as spamming the same links across multiple subreddits, submitting posts or comments that receive a high number of downvotes, a lack of recent account activity, or having an unverified account.

Please be assured that this action is not a reflection of your participation in our subreddit. This is simply an automated filter in place to reduce spam.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.