iOS Facebook 登录 - 链接器错误

Posted

技术标签:

【中文标题】iOS Facebook 登录 - 链接器错误【英文标题】:iOS Facebook login - Linker errors 【发布时间】:2013-03-20 14:21:10 【问题描述】:

我正在尝试在我的应用中实现 facebook 登录,我已遵循以下两个指南:

http://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/ https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/

但是当我尝试为 iPhone 模拟器编译应用程序时,我得到一个链接器错误。

Undefined symbols for architecture i386:
  "_FBSessionStateChangedNotification", referenced from:
      -[AppDelegate sessionStateChanged:state:error:] in AppDelegate.o
      -[LoginViewController viewDidLoad] in LoginViewController.o
      -[AppDelegate sessionStateChanged:state:error:] in AppDelegate.o
      -[LoginViewController viewDidLoad] in LoginViewController.o
ld: symbol(s) not found for architecture i386

我已确保所有必要的框架和文件都在正确的位置。另外,我在 Xcode 中没有代码错误,所以我真的不知道这里有什么问题。

拜托,非常感谢任何帮助。


编辑:

//AppDelegate.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> 



@property (strong, nonatomic) UIWindow *window;

extern NSString *const FBSessionStateChangedNotification;
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
- (void)closeSession;
@end

-

//  AppDelegate.m

#import "AppDelegate.h"
#import "NetworkCheck.h"


@implementation AppDelegate

- (void)dealloc

    [_window release];
    [super dealloc];


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // Override point for customization after application launch.
    NSLog(@"Application launch");

    NetworkCheck *netCheck = [[NetworkCheck alloc] init];
    [netCheck startCheck];

    NSString *const FBSessionStateChangedNotification =     @"com.example.Login:FBSessionStateChangedNotification";


    return YES;


/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
 */
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation 
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];


/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error

    switch (state) 
        case FBSessionStateOpen:
            if (!error) 
                // We have a valid session
                NSLog(@"User session found");
            
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:[FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    

[[NSNotificationCenter defaultCenter]
 postNotificationName:FBSessionStateChangedNotification
 object:session];

if (error) 
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:error.localizedDescription
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];



/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI 
return [FBSession openActiveSessionWithReadPermissions:nil
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState state,
                                                         NSError *error) 
                                         [self sessionStateChanged:session
                                                             state:state
                                                             error:error];
                                     ];


- (void) closeSession 
[FBSession.activeSession closeAndClearTokenInformation];


- (void)applicationWillResignActive:(UIApplication *)application

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


- (void)applicationDidEnterBackground:(UIApplication *)application

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


- (void)applicationWillEnterForeground:(UIApplication *)application

// Called as part of the transition from the background to the inactive state; here you     can undo many of the changes made on entering the background.


- (void)applicationDidBecomeActive:(UIApplication *)application

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

// We need to properly handle activation of the application with regards to Facebook Login
// (e.g., returning from iOS 6.0 Login Dialog or from fast app switching).
[FBSession.activeSession handleDidBecomeActive];


- (void)applicationWillTerminate:(UIApplication *)application

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[FBSession.activeSession close];


@end

【问题讨论】:

【参考方案1】:

FBSessionStateChangedNotification 是一个extern 定义,它应该在您正在导入的任何头文件中可见,以便您在 appDelegate 中编写的代码可以访问它。

你需要转移这条线:

NSString *const FBSessionStateChangedNotification = @"com.example.Login:FBSessionStateChangedNotification";

到一个文件,使其对其他类(不仅是应用程序委托)可见。

【讨论】:

谢谢,我已将 AppDelegate.h 和 .m 添加到我的问题中。 我在视图控制器中有 facebook 登录按钮,在视图控制器中我导入 AppDelegate.h 谢谢,“一个可以让其他类看到的文件”什么文件? AppDelegate 是由我的登录视图控制器导入的,那么它不可见吗?我真的很感谢你的帮助:) 定义本身必须在 .h 文件中,而不是在方法体中。我建议只为这个特定的字符串做一个#define,因为它是静态的。 我将行移到应用程序 didFinishLaunchingWithOptions 方法之外,现在一切正常。谢谢:)

以上是关于iOS Facebook 登录 - 链接器错误的主要内容,如果未能解决你的问题,请参考以下文章

facebook SDK3.1 和 Xcode 4.5 链接器错误

构建登录演示时的链接器错误

Firebase iOS Facebook 简单登录错误

链接器错误 Facebook 解析

应用程序在设备上显示 facebook SDK 的链接器错误

使用Facebook,Gmail和其他注册链接在iOS应用程序中设置登录/注册屏幕的最佳/最简单方法是什么?