简体   繁体   中英

Facebook iOS SDK : Login with Facebook failed

after I read few references on the internet, I decided to use Facebook SDK for iOS instead of XCode Social Framework .

Because what I need on my app is 'login with facebook' session and I'm following its sample code named SessionLoginSample under Sample folder but it doesn't work when I put on my code. These code was build successfully on iPad simulator but when I click the button, nothing happen.

Could you please show me where did I miss here... thank you...

I have this AppDelegate.h :

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@property (strong, nonatomic) FBSession *session;

@end

and here's my AppDelegate.m :

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize session = _session;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBAppEvents activateApp];
    [FBAppCall handleDidBecomeActiveWithSession:self.session];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self.session close];
}


- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBAppCall handleOpenURL:url
                  sourceApplication:sourceApplication
                        withSession:self.session];
}

#pragma mark Template generated code

@end

my ViewController.h :

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
@property (strong, nonatomic) IBOutlet UILabel *titleLogin;
@property (strong, nonatomic) IBOutlet UILabel *labelOutput;

- (IBAction)buttonRegister;
- (IBAction)buttonLogin;
- (IBAction)loginFacebook;
- (IBAction)loginTwitter;

@end

my ViewController.m :

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()

@property (strong, nonatomic) UINavigationController* navController;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
                                                      message:msg
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}

- (IBAction)loginFacebook {
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

    if (appDelegate.session.isOpen) {
        [appDelegate.session closeAndClearTokenInformation];

    } else {
        if (appDelegate.session.state != FBSessionStateCreated) {
            appDelegate.session = [[FBSession alloc] init];
        }

        [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            [self alertWithTitle:(@"FB Login") andMessage:(@"It works!!")];
        }];
    } }
}
@end

UPDATE : I have add FacebookSDK.framework under Framework folder on my project, also modified the plist file based on this introduction too :

In my view you are mission a call like -setActiveSession in

if (appDelegate.session.state != FBSessionStateCreated) 
{
   appDelegate.session = [[FBSession alloc] init];
   [FBSession setActiveSession:appDelegate.session];
}

I had the same problem integrating iOS Facebook sdk 3.5 a month ago, And I found out that your current active session doesn't set automatically. So I have this call wherever there is a chance of getting a different session instance.

Also,

inside

    - (void)applicationDidBecomeActive:(UIApplication *)application
   {
      [FBAppEvents activateApp];
      [FBAppCall handleDidBecomeActiveWithSession:self.session];
   }

instead of self.session, you should use FBSession.activeSession just to ensure that the FBAppCall gets the same instance of FBSession, for which it has got the result.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM