开发者

How to use Facebook Login in a modal view in iOS SDK?

开发者 https://www.devze.com 2023-03-22 10:16 出处:网络
I\'m trying to modify the demo that comes with the Facebook iOS SDK. I need the login button appears in a second modal view.

I'm trying to modify the demo that comes with the Facebook iOS SDK. I need the login button appears in a second modal view.

When I click on the login button, it opens Facebook App and authorization is sought, then returns to my app but fbDidLogin is not fired.

I have followed all the steps from here https://github.com/facebook/facebook-ios-sdk.

Here is the code i'm using. Please help me to find out what i'm missing.

I'm using Xcode 4.1 and 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;
}
开发者_如何学JAVA
- (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


First of all, how does this even compile?

@synthesize firstViewViewController = _ firstViewViewController;

You don't have _firstViewController declared anywhere.
Secondly, I think you are mixing views and view controllers.
Finally, your view controller that handles FB authorization (loginView) should implement

FBRequestDelegate,FBDialogDelegate,FBSessionDelegate

So your loginView.h should look something like:

@interface loginView : UIViewController < FBRequestDelegate,FBDialogDelegate,FBSessionDelegate>{

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号