简体   繁体   中英

Facebook iOS SDK 3.0 - session not open

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

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:(
    )>

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];
}

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