Swift 4+Xcode9.3集成Google登录iPad9.3跳转网页问题求助
Hey there, let's troubleshoot why your Google Sign-In works smoothly on iOS 11.3 iPhone but jumps to the web on iOS 9.3 iPad. Here are the key fixes to resolve this:
1. Ensure Proper URL Scheme & Query Scheme Configuration
iOS 9 requires explicit declaration of URL schemes in your Info.plist for app-to-app communication—this is often the root cause of web fallback:
- Open your
Info.plistas source code and add the following blocks (replaceYOUR_GOOGLE_CLIENT_IDwith your actual client ID from the Google Cloud Console):<!-- URL Scheme for Google Sign-In callback --> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.YOUR_GOOGLE_CLIENT_ID</string> </array> </dict> </array> <!-- Allow querying Google's apps (required for iOS 9) --> <key>LSApplicationQueriesSchemes</key> <array> <string>googlechrome</string> <string>googlechromes</string> <string>com.googleusercontent.apps.YOUR_GOOGLE_CLIENT_ID</string> </array>
2. Update AppDelegate for iOS 9 Compatibility
iOS 10 introduced a new open url delegate method, but iOS 9 relies on the older one. Make sure you implement both to cover all versions:
- Add this iOS 10+ method alongside your existing code:
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool { return GIDSignIn.sharedInstance().handle( url, sourceApplication: options[.sourceApplication] as? String, annotation: options[.annotation] ) } - Fix your existing iOS 9 method to pass the
annotationparameter correctly (your snippet had an ellipsis):func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { let googleDidHandle = GIDSignIn.sharedInstance().handle( url, sourceApplication: sourceApplication, annotation: annotation ) return googleDidHandle }
3. Use a Google Sign-In SDK Version Compatible with iOS 9
Newer versions of the Google Sign-In SDK drop support for iOS 9. Double-check your dependency setup:
- If using CocoaPods, ensure your Podfile specifies a version that supports iOS 9:
pod 'GoogleSignIn', '~> 4.0' # Version 4.x maintains iOS 9 compatibility - Run
pod update GoogleSignInto install the latest compatible version.
4. Confirm Native App Preference is Enabled
By default, Google Sign-In prefers the native Google app if installed, but it's good to set this explicitly in your app's launch setup:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { GIDSignIn.sharedInstance().clientID = "YOUR_GOOGLE_CLIENT_ID" // Ensure basic profile fetch is enabled (default is true, but explicit avoids surprises) GIDSignIn.sharedInstance().shouldFetchBasicProfile = true return true }
Most often, missing URL scheme configuration or incomplete delegate methods are the culprits here. Give these steps a test and let me know if you still hit issues!
内容的提问来源于stack exchange,提问作者Raj

