Facebook iOS sdk xcode 4.3/storyboards

2020-07-23 08:57发布

问题:

i have been looking at tutorials for weeks now, not been able to find ANY storyboard examples implementing FB in iOS.

i got my app to open the FBloginscreen, request user auth for app, and then return to app... like some other users , fbdidlogin and handleopenurl method are not called. i am sure i am doing something wrong but just don't know what. i am using the default view controller in my storyboard (i did not create a vc programatically ) so i might need to do something with that.

from what i understand that handleopenurl method needs to be in my app delegate.m file and not in my view controller.m file, but i am not sure how it should be written and how to connect a UIViewController object i create to the VC in my storyboard (the one with the buttons and labels that i am using).

ViewController.h (relevant to FB)

#import "FBConnect.h"

@interface ViewController : UIViewController
<UIImagePickerControllerDelegate, 
UINavigationControllerDelegate, FBSessionDelegate, FBDialogDelegate>
{
Facebook *facebook; 
}

@property (nonatomic,retain) Facebook *facebook;'

ViewController.m (relevant to FB)

#import "ViewController.h"

@implementation ViewController;
@synthesize facebook;   

-(void)LogintoFB
{
    facebook = [[Facebook alloc] initWithAppId:@"345872345487883" 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];
        NSLog(@"did log in");
    }


}
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"-4.2 got calleld");
    return [facebook handleOpenURL:url]; 
}
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSLog(@"4.2+ got called");
    return [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];


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


}'

also if you can direct me to a tutorial that used storyboard when implementing Facebook in iOS that would be great.

thank you!

回答1:

-(BOOL)application:openURL:sourceApplication:annotation: should be implemented in the app delegate.

Add a property to the appDelegate :

@property (nonatomic, strong) Facebook *facebook;

Synthesize it!

In your logintoFB method add this after creating the Facebook object (AppDelegate should be whatever your appDelegate class name is)

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.facebook = facebook;