2

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!

4

1 回答 1

0

-(BOOL)application:openURL:sourceApplication:annotation:应该在应用程序委托中实现。

向 appDelegate 添加一个属性:

@property (nonatomic, strong) Facebook *facebook;

合成它!

在您的 logintoFB 方法中,在创建 Facebook 对象后添加它(AppDelegate 应该是您的 appDelegate 类名称)

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.facebook = facebook;
于 2012-08-05T03:18:02.227 回答