Swift构建失败:未知类名‘FBSDKLoginButtonDelegate’错误求助
Hey there, let's tackle this FBSDKLoginButtonDelegate unknown class error you're hitting in your Unity-Swift Facebook login integration. I've run into similar Swift-Objective-C bridging issues with Unity-exported Xcode projects, so here are the most actionable fixes to try:
1. Verify FBSDK Dependencies Are Properly Set Up
First, make sure the Facebook SDK is fully integrated in your Xcode project:
- If you're using CocoaPods, double-check your
Podfileincludespod 'FBSDKLoginKit'(alongsideFBSDKCoreKitif needed). Runpod installagain to ensure all frameworks are up-to-date and linked. - If you're manually adding frameworks, confirm
FBSDKLoginKit.frameworkis added to Build Phases > Link Binary With Libraries and that its path is correctly set in Build Settings > Framework Search Paths.
2. Fix the Swift-to-Objective-C Header Generation
The DemoApp-Swift.h file is auto-generated by Xcode, and misconfigured build settings often break this:
- Go to your project's Build Settings and search for these options:
- Set
Objective-C Generated Interface Header Nameto exactlyDemoApp-Swift.h(match the name you're importing). - Toggle
Install Objective-C Compatibility Headerto Yes.
- Set
- Clean your project thoroughly: Press
Cmd+Shift+Kto clean the build folder, thenCmd+Option+Shift+Kto delete Derived Data. Rebuild the project after this.
3. Make Your Swift Code Visible to Objective-C
By default, Swift classes and methods are internal (only accessible within the Swift module), which means Objective-C can't see them:
- Update your
LoginViewControllerdefinition to bepublic:public class LoginViewController: UIViewController, FBSDKLoginButtonDelegate { // Your existing code here } - Also mark the
FBSDKLoginButtonDelegaterequired methods aspublicso they're exposed to Objective-C:public func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { // Your login completion logic } public func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) { // Your logout logic }
4. Account for Unity-Specific Configuration Quirks
Unity's Xcode export can sometimes overwrite or tweak build settings, so double-check these:
- In Unity's Player Settings > iOS > Configuration, set the
Scripting Backendto IL2CPP (this has better compatibility with Swift and native frameworks than Mono). - After exporting from Unity, re-run
pod install(if using CocoaPods) to re-link FBSDK frameworks—Unity sometimes resets these links during export.
5. Explicitly Import FBSDK in Objective-C Files
If DemoApp-Swift.h still can't resolve the delegate, import the FBSDK header directly in any Objective-C file that references DemoApp-Swift.h:
#import <FBSDKLoginKit/FBSDKLoginKit.h> #import "DemoApp-Swift.h"
This ensures the FBSDKLoginButtonDelegate definition is available before Xcode parses the auto-generated Swift header.
内容的提问来源于stack exchange,提问作者Gamsh

