如何在iOS SDK的模态视图中使用Facebook登录?

时间:2011-07-25 00:45:24

标签: ios facebook ios4

我正在尝试修改Facebook iOS SDK附带的演示。 我需要登录按钮出现在第二个模态视图中。

当我点击登录按钮时,它会打开Facebook App并寻求授权,然后返回我的应用程序,但不会触发fbDidLogin。

我已按照https://github.com/facebook/facebook-ios-sdk的所有步骤进行操作。

这是我正在使用的代码。请帮我找出我错过的东西。

我正在使用Xcode 4.1和iOS SDK 4.3

myAppAppDelegate.h

#import <UIKit/UIKit.h>
#import "loginView.h"
#import "firstView.h"

@class firstView;

@interface myAppAppDelegate : NSObject <UIApplicationDelegate> {
    firstView* controller;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet firstView *firstViewViewController;

@end

myAppAppDelegate.m

#import "firstView.h"
#import "loginView.h"

@synthesize window;

@synthesize firstViewViewController = _ firstViewViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    controller = [[firstView alloc] init];
    firstView *firstView = [[firstView alloc] initWithNibName:nil bundle:nil];
    [window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

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

- (void)dealloc {
    [window release];
    [_firstViewViewController release];
    [controller release];
    [super dealloc];
}

@end

firstView.h

#import <UIKit/UIKit.h>

@interface firstView : UIViewController

-(IBAction)openLoginView:(id)sender;

@end

firstView.m

#import "loginView.h"

@implementation firstView

-(IBAction)openLoginView:(id)sender{
    loginView *loginView = [[loginView alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:loginView animated:YES];
}

@end

loginView.h

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

@interface loginView : UIViewController<FBRequestDelegate,
FBDialogDelegate,
FBSessionDelegate>{
    NSArray* _permissions;
    Facebook* _fb;
}
@property(readonly) Facebook *fb;
-(IBAction)fbButtonClick:(id)sender;

@end

loginView.m

#import "loginView.h"

static NSString* kAppId = @"260718013943480";

@implementation loginView

@synthesize fb = _fb;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (!kAppId) {
        NSLog(@"missing app id!");
        exit(1);
        return nil;
    }
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _permissions =  [[NSArray arrayWithObjects:
                          @"read_stream", @"publish_stream", @"offline_access",nil] retain];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    _fb = [[Facebook alloc] initWithAppId:kAppId];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)login {
    [_fb authorize:_permissions delegate:self];
}

- (IBAction)fbButtonClick:(id)sender {
    NSLog(@"login button clicked");
    [self login];
}

- (void)fbDidLogin {
    NSLog(@"logged in");
}

/**
 * Called when the user canceled the authorization dialog.
 */
-(void)fbDidNotLogin:(BOOL)cancelled {
    NSLog(@"did not login");
}

/**
 * Called when the request logout has succeeded.
 */
- (void)fbDidLogout {
    NSLog(@"logedout");
}

@end

1 个答案:

答案 0 :(得分:2)

首先,这甚至是如何编译的?

@synthesize firstViewViewController = _ firstViewViewController;

你没有在任何地方声明_firstViewController 其次,我认为你正在混合观点和视图控制器 最后,处理FB授权(loginView)的视图控制器应该实现

  

FBRequestDelegate,FBDialogDelegate,FBSessionDelegate

所以你的loginView.h应该是这样的:

  

@interface loginView:UIViewController&lt;   FBRequestDelegate,FBDialogDelegate,FBSessionDelegate&GT; {