iOS 10下Google Sign-in异常重定向至谷歌首页问题排查
Hey there, let's figure out why your Google Sign-in is misbehaving on iOS 10 while working fine on iOS 11, and how to fix it:
1. SFSafariViewController Callback Handling Differences
iOS 10 and iOS 11 have subtle differences in how SFSafariViewController handles callbacks. On iOS 10, the Google Sign-in SDK might fail to properly catch the authorization completion signal, leading it to redirect to Google's homepage instead of your app's follow-up flow.
- Fix Steps:
- Make sure you've correctly implemented the
application(_:open:options:)method (orapplication(_:open:sourceApplication:annotation:)for iOS 9 and earlier) and that you're callingGIDSignIn.sharedInstance().handle(url)inside it to process the callback URL. - Double-check your
Info.plistfor the correct URL Scheme, which should be in the formatcom.googleusercontent.apps.{YOUR_CLIENT_ID}. This must match the client ID you registered in the Google Cloud Console.
- Make sure you've correctly implemented the
2. Google Sign-in SDK Version Compatibility
Newer versions of the Google Sign-in SDK might not have full support for iOS 10, or certain APIs behave differently on iOS 10 compared to iOS 11+.
- Fix Steps:
- Try downgrading to an SDK version that explicitly supports iOS 10, like the
GoogleSignIn5.x series (note: 5.x officially supports iOS 9+). - If you're using CocoaPods, specify the version in your
Podfile:pod 'GoogleSignIn', '~> 5.0.2' - Run
pod installto update your dependencies afterward.
- Try downgrading to an SDK version that explicitly supports iOS 10, like the
3. Redirect URI Configuration Issues
The redirect URI you set up in the Google Cloud Console might not be properly adapted for iOS 10's behavior. iOS 10 has stricter handling for custom URL schemes, or the redirect URI isn't pointing correctly to your app.
- Fix Steps:
- Log into the Google Cloud Console, navigate to your project's APIs & Services > Credentials page.
- Verify that the redirect URI for your iOS client is set to
{YOUR_URL_SCHEME}:/oauth2callback(e.g.,com.googleusercontent.apps.1234567890:/oauth2callback). - Ensure there are no extra spaces or typos, save the changes, and retest.
4. App Transport Security (ATS) Restrictions
While iOS 11 also enforces ATS, iOS 10's rules might be stricter, causing the post-login redirect to get blocked—resulting in a fallback to Google's homepage.
- Fix Steps:
- Add ATS exceptions in your
Info.plistto allow communication with Google-related domains. Here's the configuration to add:<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>google.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionAllowsInsecureHTTPLoads</key> <false/> <key>NSExceptionRequiresForwardSecrecy</key> <true/> </dict> <key>googleapis.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionAllowsInsecureHTTPLoads</key> <false/> <key>NSExceptionRequiresForwardSecrecy</key> <true/> </dict> </dict> </dict> - Note: Avoid disabling ATS entirely—only add exceptions for the domains you need.
- Add ATS exceptions in your
内容的提问来源于stack exchange,提问作者bigreddawg

