2012-08-19 159 views
1

我一步一步跟着Facebook上的Facebook SDK教程。除了fbDidLogin被调用之外,我还有一些工作...授权过程正常工作。有人告诉我做:IOS - Facebook SDK fbDidLogin未调用 - 初始化视图控制器。

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

fbDidLogin not called

问题是,我不完全知道如何初始化页头我的视图控制器。这里是我的应用程序的委托.H:

#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!

+0

你是什么意思的第二个版本的实施? – stackOverFlew 2012-08-19 05:42:32

+0

虽然...不起作用,但...给出以下错误: 不可见@interface for'UIViewController'声明选择器'Facebook'=( – stackOverFlew 2012-08-19 05:50:00

+1

[iphone SSO Facebook实现]的可能重复(http:// stackoverflow .com/questions/10403697/iphone-sso-facebook-implementation) – CodaFi 2012-08-19 05:51:55

回答

2

好吧,你需要做的:

[facebook handleOpenURL:url]. 

您正在用facebook的选择,而不是一个对象。

另外..你需要设置facebook.sessionDelegate = self,这样委托方法fbDidLogin可以被调用。

希望这会有所帮助!

+1

+1,这个小时的男人! – CodaFi 2012-08-19 06:12:35

+0

@ user1392515感谢您给我正确的答案。我并没有真正希望你说实话。我只是喜欢在Im 100%确信它解决了你的问题之前不回答,因为它可能会阻止其他人在你已经回答的时候阅读你的问题。 – 2012-08-19 06:26:18

+0

@CodaFi同样适用于你:) – 2012-08-19 06:26:35

2

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

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

还请记住,为了委托方法被调用,您的对象实际上必须是一个委托。因此请设置facebook.sessionDelegate = self,并使用<FBSessionDelegate>标头中的协议来使编译器警告无效。

+0

thx为答案!首先尝试,没有发生任何事......我的意思是说,当然有意义..但fbDidLogin永远不会被调用... – stackOverFlew 2012-08-19 06:01:29

+0

你知道为什么...? – stackOverFlew 2012-08-19 06:03:35

+1

当然不是,傻了。你不是这个东西的'sessionDelegate'! – CodaFi 2012-08-19 06:04:21

相关问题