IOS - 未调用Facebook SDK fbDidLogin - 初始化视图控制器。

时间:2012-08-19 05:32:26

标签: ios facebook sdk

我一步一步地从Facebook上关注Facebook SDK教程。除了调用fbDidLogin之外我还能正常工作......授权过程正常。我被告知要做:

     - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
          return [[controller facebook] handleOpenURL:url];
     }

来自fbDidLogin not called

问题是我不确定如何分配init我的视图控制器。这是我的.h for app delegate:

#import <UIKit/UIKit.h>
#import "FBConnect.h"


@interface fb2AppDelegate : UIResponder  <UIApplicationDelegate, FBSessionDelegate> {

    Facebook *facebook; 
    UIViewController *fb2ViewController;
}

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) Facebook *facebook; 
@property (nonatomic, strong) UIViewController *fb2ViewController;

@end

和.m:

#import "fb2AppDelegate.h"

@implementation fb2AppDelegate
@synthesize window = _window;
@synthesize facebook;
@synthesize fb2ViewController;


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

    self.fb2ViewController = [[UIViewController alloc] init];

    facebook = [[Facebook alloc] initWithAppId:@"key_here" andDelegate:self];


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if (![facebook isSessionValid]) {
        [facebook authorize:nil];
    } 


    return YES;
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [[fb2ViewController facebook] handleOpenURL:url];
}


- (void)fbDidLogin {

    NSLog(@"fbdidlogin"); 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"key_here", @"app_id",
                                   @"http://developers.facebook.com/docs/reference/dialogs/", @"link",
                                   @"http://fbrell.com/f8.jpg", @"picture",
                                   @"Facebook Dialogs", @"name",
                                   @"Reference Documentation", @"caption",
                                   @"Using Dialogs to interact with users.", @"description",
                                   nil];

    [facebook dialog:@"feed" andParams:params andDelegate:self];

}

至于视图控制器......我没有在其中添加任何代码。

我收到的错误是:

'UIViewController'没有可见的@interface声明选择器'Facebook'

我在做错了什么?

THX!

2 个答案:

答案 0 :(得分:2)

问题是运行时认为你的UIViewController有类方法+ facebook,实际上,这是一个单例,其他问题的答案使用(相当混乱,我可能会添加)。您需要将其发送到Facebook 对象的实例并从那里调用您的代码。

 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
      return [facebook handleOpenURL:url];
 }

还要记住,为了调用委托方法,您的对象实际上必须是委托。因此设置facebook.sessionDelegate = self并使用<FBSessionDelegate>符合标头中的协议,以使编译器警告静音。

答案 1 :(得分:2)

好的,你需要这样做:

[facebook handleOpenURL:url]. 

您当前正在使用facebook作为选择器而不是对象。

此外..您需要设置facebook.sessionDelegate = self,以便可以调用委托方法fbDidLogin

希望这有帮助!