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

Swift开发问题:IBOutlet连接断开及Firebase Gmail登录按钮异常

Hey there! Let's work through your two Swift development problems one at a time—they’re both super common, so we’ve got this!

Issue 1: IBOutlet Button Connections Keep Breaking

If your button’s IBOutlet keeps disconnecting after building, try these targeted fixes:

  • Double-check your view controller’s Custom Class in the Storyboard’s Identity Inspector. Make sure the class name is spelled correctly, and the module is set to your project (not "None"—this is a frequent culprit when working with CocoaPods dependencies).
  • Hunt down duplicate or stale connections: Right-click the button in Storyboard, open the Connections Inspector, and delete any old/duplicate references to your IBOutlet. You can also right-click your view controller class file in the Project Navigator and check the Connections panel to remove invalid links.
  • Clear your project’s cache: Hit Cmd + Shift + K to clean the build folder, then Cmd + Option + Shift + K to wipe derived data. Restart Xcode after this—sometimes old cached data causes weird connection glitches.
  • Avoid mismatched names: If you rename the IBOutlet variable in code, don’t forget to update the connection in Storyboard. It’s often easier to just delete the old connection and re-drag from the button to your code file.
  • Check if the button’s parent view is accidentally hidden or removed: If the button’s superview is set to isHidden = true or gets removed from the hierarchy, Xcode might drop the connection as it thinks the button isn’t active.
Issue 2: GIDSignInButton Setup Problems with Firebase Gmail Login

Let’s fix the GIDSignInButton subclass issue and get your Google login working smoothly:

First, rule out dependency issues

Make sure you’ve properly installed Firebase and GoogleSignIn. If you’re using CocoaPods, your Podfile should include:

pod 'Firebase/Auth'
pod 'GoogleSignIn'

Run pod install and open the .xcworkspace file (not the .xcodeproj—this is a common mistake that breaks framework links!).

Option 1: Create the button with code (more reliable)

Skip Storyboard altogether for this button—code-based setup avoids subclass bugs entirely:

import UIKit
import GoogleSignIn
import FirebaseAuth

class LoginViewController: UIViewController, GIDSignInDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create and position the Google Sign-In button
        let signInButton = GIDSignInButton()
        signInButton.frame = CGRect(x: 20, y: 300, width: view.frame.width - 40, height: 50)
        view.addSubview(signInButton)
        
        // Configure Google Sign-In
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().presentingViewController = self
        // Optional: Auto-sign in if user was previously logged in
        GIDSignIn.sharedInstance().restorePreviousSignIn()
    }
    
    // Handle Google Sign-In result
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("Google Sign-In failed: \(error.localizedDescription)")
            return
        }
        
        // Pass Google credentials to Firebase
        guard let auth = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: auth.idToken, accessToken: auth.accessToken)
        
        Auth.auth().signIn(with: credential) { [weak self] result, error in
            guard let self = self else { return }
            if let error = error {
                print("Firebase authentication failed: \(error.localizedDescription)")
                return
            }
            // Success! Navigate to your main app screen here
        }
    }
}

Option 2: Fix Storyboard subclass setup

If you really want to use Storyboard:

  • Drag a regular UIButton onto your view, then go to the Identity Inspector.
  • In the Class field, type GIDSignInButton exactly (capitalization matters!) and select GoogleSignIn from the Module dropdown (if it doesn’t appear, double-check that GoogleSignIn is linked in Build Phases > Link Binary With Libraries).
  • If the subclass still won’t stick, close Xcode, delete the derived data folder (you can find it via Xcode > Preferences > Locations > Derived Data), then reopen your project.

Don’t forget AppDelegate setup

Add this to your AppDelegate.swift to handle the Google Sign-In redirect:

import UIKit
import Firebase
import GoogleSignIn

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }
}

Final check: Info.plist URL Scheme

Make sure you’ve added your Google Cloud Console’s REVERSED_CLIENT_ID as a URL Scheme in your Info.plist. You can do this by adding a new row with CFBundleURLTypes, then adding an item with CFBundleURLSchemes containing your reversed client ID.

内容的提问来源于stack exchange,提问作者Dedeepya Reddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:07:30