IOS - Facebook SDK fbDidLogin not called — initialize view controllers.

大憨熊 提交于 2019-12-23 03:06:49

问题


I followed the Facebook SDK tutorial from Facebook step by step. I have everything working besides fbDidLogin getting called... the authorization process works fine. I've been told to do:

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

from fbDidLogin not called

the problem is is that I'm not exactly sure how to alloc init my view controller. Here is my .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

and the .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];

}

as for the view controllers... I haven't put any code in them.

the error that I am receiving is:

no visible @interface for 'UIViewController' declares the selector 'Facebook'

what am i doing wrong?

thx!


回答1:


Okay, You need to do:

[facebook handleOpenURL:url]. 

You are currently using facebook as a selector instead of an object.

Also.. you need to set facebook.sessionDelegate = self, so that the delegate method fbDidLogin can get called.

Hope this helps!




回答2:


The problem is that the runtime believes that your UIViewController has the class method +facebook, when in reality, that was an example singleton the other question's answer used (rather confusingly I might add). You need to send it to an instance of the Facebook object and call your code from there.

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

Also remember that in order for delegate methods to be called, your objects actually has to be a delegate. So set facebook.sessionDelegate = self and conform to the protocol in the header with <FBSessionDelegate> to silence the compiler warnings.



来源:https://stackoverflow.com/questions/12024446/ios-facebook-sdk-fbdidlogin-not-called-initialize-view-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!