You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OAuth2通过GitHub登录时应配置何种重定向URL?

Fixing GitHub OAuth2 Redirect for iOS with OAuthSwift

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:


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 format applinks:yourdomain.com (replace yourdomain.com with 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 your Info.plist for you.
  • Host the Apple App Site Association File
    Create a file named apple-app-site-association (no file extension) with this structure, then host it at https://yourdomain.com/.well-known/apple-app-site-association:
    {
      "applinks": {
        "apps": [],
        "details": [
          {
            "appID": "YOUR_TEAM_ID.YOUR_BUNDLE_ID",
            "paths": ["/github-callback/*"]
          }
        ]
      }
    }
    
    Replace YOUR_TEAM_ID with your Apple Developer team ID and YOUR_BUNDLE_ID with your app’s bundle ID.
  • Update GitHub & OAuthSwift
    Set your GitHub app’s redirect URL to https://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 (like application(_: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 your Info.plist, add a URL types entry. Set the URL identifier to your bundle ID, and add a unique URL Schemes value (like yourapp-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 valid https URL (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 the https web 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 (like application(_: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 app or github) 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:44:59