4

I am using the latest Facebook iOS SDK 3.0

I need a help in in the login process:

First I declare this property in AppDelegate.h:

@property (nonatomic, strong) FBSession *session;

and in ViewController class I get this to use it in the code as this:

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[delegate.session someproperty];

I also have this method in ViewController that get called from viewDidLoad:

-(void)login
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    [delegate.session accessToken];
    NSLog(@"%d",delegate.session.isOpen);
    if (!delegate.session.isOpen)
    {
        delegate.session = [[FBSession alloc] init];
        if (delegate.session.state == FBSessionStateCreatedTokenLoaded)
        {
            [delegate.session openWithCompletionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) 
            {
              NSLog(@"%d", delegate.session.isOpen); // First Line //////////////////
            }];
        }
        NSLog(@"%@", delegate.session.description);   // Second Line //////////////////
    }
 }

After the facebook app get authorized the firs NSLog print zero, and the second NSLog indicate that the session state is FBSessionStateCreated not FBSessionStateOpen?

this is the output for the second NSLog:

2012-08-16 18:37:24.327 Facebook3[2418:f803] <FBSession: 0x6890ff0, state:    FBSessionStateCreated, loginHandler: 0x0, appID: 193716877424306, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x6890f20>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:()>

What I am missing here.

Note : in the app setting at https://developers.facebook.com I configure the app as this: 1- Configured for iOS SSO: Enabled 2- iOS Native Deep Linking: Enabled 3- iOS Bundle ID : com.mycompany.appname

gran33
  • 12,421
  • 9
  • 48
  • 76
MohamMad Salah
  • 971
  • 2
  • 14
  • 31

2 Answers2

5

i used this framework for my project. it works properly. this is my related code

   -(IBAction)logIn:(id)sender;
{
    AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (!FBSession.activeSession.isOpen) {
        [appdelegate sessionOpener];
    }
    else {
        [self loginHelp];
    }

and my sessionOpener function is;

    -(void) sessionOpener
{

    [FBSession sessionOpenWithPermissions:nil
                        completionHandler:^(FBSession *session,
                                            FBSessionState status,
                                            NSError *error) {
                            // session might now be open.
                            if (!error) {
                                [self.viewController loginHelp];
                            }
                        }];

     NSLog(@"%d", FBSession.activeSession.isOpen);
    NSLog(@"%@", FBSession.activeSession.description );
}

it works for me. may be helpful to you.

and my console print is:

     1
2012-08-16 22:24:55.899 TaraftarlikOyunu[838:c07] <FBSession: 0xd2512c0, state: FBSessionStateOpen, loginHandler: 0xd250240, appID: 433294056715040, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xd24fda0>, expirationDate: 2012-10-15 19:02:34 +0000, refreshDate: 2012-08-16 19:05:03 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
    )>
meth
  • 1,887
  • 2
  • 18
  • 33
  • thnx for your answer I will try it ,,, but is the different between using `FBSession` and instance of `FBSession` class? – MohamMad Salah Aug 16 '12 at 18:08
  • have you got a - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error – meth Aug 16 '12 at 18:12
  • Yes I have one in the `ViewController` class but it never get called, do you know why? – MohamMad Salah Aug 16 '12 at 18:35
  • it should be in the appdelegate. because you define the session in appdelegate. – meth Aug 16 '12 at 18:37
  • ok. did you make request. can you get any information about session. – meth Aug 16 '12 at 18:54
  • I don't know what do you mean with request, my current session state is `FBSessionStateOpen` ,,, note that these operation done in `ViewController` – MohamMad Salah Aug 16 '12 at 19:02
  • sorry for misunderstanding. i am checking whether the session is opened or not with NSLog(@"%d", FBSession.activeSession.isOpen); it prints 1. – meth Aug 16 '12 at 19:17
  • i guess after creating the session it makes the session active. and you can check it with this code. i edited my code – meth Aug 16 '12 at 19:23
  • @meth can u take a look at this http://stackoverflow.com/questions/14414118/how-to-close-active-session-of-facebook-ios-sdk – Christien Jan 19 '13 at 12:20
  • Is this answer outdated? There seems to be no such method now. – Katedral Pillon Mar 18 '15 at 00:14
2

I had the same problem as you and I had mixed between using FBSession and instance of FBSession when calling handleOpenURL. I changed from

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
    [FBSession.activeSession facebookSession handleOpenURL:url];
}

to this

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
    [session facebookSession handleOpenURL:url];
}
Daniel Nord
  • 535
  • 5
  • 10