使用OAuth2通过GitHub登录时应配置何种重定向URL?
Hey there! No need to apologize at all—we’ve all been there as newbies, and language barriers don’t stop us from solving problems together. Let’s break down how to fix this GitHub OAuth2 redirect issue so you can get users back to your app after authentication.
The Core Problem
GitHub requires redirect URLs to be valid http/https addresses (it rejects custom schemes like com.yourapp.xyz directly), but those web URLs won’t automatically jump back to your app. We have two solid solutions for this:
Solution 1: Use Universal Links (Recommended for Clean User Experience)
Universal Links let you map a valid https URL directly to your iOS app, so GitHub’s redirect will open your app instead of a browser. Here’s how to set it up:
- Configure Associated Domains in Apple Developer Portal
Go to your app’s identifier settings, enable "Associated Domains", and add a domain in the formatapplinks:yourdomain.com(replaceyourdomain.comwith your actual domain). - Add Associated Domains to Your Project
In Xcode’s "Signing & Capabilities" tab for your app, add the "Associated Domains" capability and paste the same domain from the developer portal. Xcode will auto-update yourInfo.plistfor you. - Host the Apple App Site Association File
Create a file namedapple-app-site-association(no file extension) with this structure, then host it athttps://yourdomain.com/.well-known/apple-app-site-association:
Replace{ "applinks": { "apps": [], "details": [ { "appID": "YOUR_TEAM_ID.YOUR_BUNDLE_ID", "paths": ["/github-callback/*"] } ] } }YOUR_TEAM_IDwith your Apple Developer team ID andYOUR_BUNDLE_IDwith your app’s bundle ID. - Update GitHub & OAuthSwift
Set your GitHub app’s redirect URL tohttps://yourdomain.com/github-callback, and use this same URL as the redirect URI in your OAuthSwift configuration. Finally, implement the Universal Link handling method in your app delegate/scene delegate (likeapplication(_:continue:restorationHandler:)) to pass the URL to OAuthSwift and finish authentication.
Solution 2: Use a Web Page Redirect Workaround (Faster Setup)
If you don’t want to deal with Universal Links, you can use a simple web page to bridge the valid https URL to your app’s custom scheme:
- Set Up a Custom URL Scheme
In yourInfo.plist, add aURL typesentry. Set theURL identifierto your bundle ID, and add a uniqueURL Schemesvalue (likeyourapp-github-auth). - Create a Redirect Web Page
Make a basic HTML file with JavaScript that auto-redirects to your custom scheme, then host it at a validhttpsURL (e.g.,https://yourdomain.com/github-redirect):<!DOCTYPE html> <html> <body> <p>Redirecting back to the app...</p> <script> window.location.href = "yourapp-github-auth://github-callback"; </script> <p>If you aren't redirected, <a href="yourapp-github-auth://github-callback">click here</a>.</p> </body> </html> - Update GitHub & OAuthSwift
Set your GitHub app’s redirect URL to thehttpsweb page URL, and use your custom scheme URL (yourapp-github-auth://github-callback) as the redirect URI in OAuthSwift. Then implement the custom URL handling method in your app delegate/scene delegate (likeapplication(_:open:options:)) to pass the URL to OAuthSwift and complete the flow.
Key Notes
- Unique Scheme: Make sure your custom URL scheme is unique (avoid generic names like
apporgithub) to prevent conflicts with other apps. - Testing: For Universal Links, test on a real device (simulators can have quirks) and use Apple’s validation tool to check your setup.
- Exact Matches: Double-check that the redirect URI in OAuthSwift matches exactly what’s set in GitHub—it’s case-sensitive!
You’ve got this—start with whichever solution feels more manageable, and let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Au Room

