15

I've been trying to develop iOS app using Facebook and I'm new. So I've been trying to make

a login with Facebook, followed a tutorial on Facebook and try to implement it.

But I've encountered, [FBSession sessionOpenWithPermissions] not found. When I run the

app, it will force close and say that error. When build the project, it will show warning

yellow exclamation that sessionOpenWithPermission is not found in FBSession

The tutorial outdated? If it is, then what is the new code for the new Facebook SDK for

sessionOpenWithPermission ?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
user1383655
  • 195
  • 1
  • 10
  • 3
    I think this is valid- I am having the same issue with the Facebook example. – Anna Billstrom Aug 09 '12 at 18:29
  • 1
    This is clearly a documentation mismatch issue. I suggest to look at the "Scrumptious" example app that ships with the SDK for up to date documentation. – Bringo Aug 10 '12 at 23:42
  • 4
    there is a mismatch in the docs tutorial and the sample. You should use `- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;` instead. – Stas Zhukovskiy Aug 14 '12 at 21:07

7 Answers7

2

Try This Code

 account = [[ACAccountStore alloc] init];
    accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    arrayOfAccounts = [account accountsWithAccountType:accountType]; 


    appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
    chk=appDelegate.chk_login;

    if (!appDelegate.session.isOpen) {
        // create a fresh session object
        appDelegate.session = [[FBSession alloc] init];
        if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
            // even though we had a cached token, we need to login to make the session usable
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                // we recurse here, in order to update buttons and labels

            }];
        }
    }
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Since `FBSession` has `activeSession` static public variable, you don't have to save the session in app delegate. – Zorayr Apr 29 '14 at 03:44
2

Maybe this code helps you, put it in your AppDelegate.m class

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {
            self.loggedinVCController = [[LoggedinVC alloc] initWithNibName:@"LoggedinVC" bundle:nil];
            self.navController = [[UINavigationController alloc]initWithRootViewController:self.loggedinVCController];
            self.window.rootViewControlle`enter code here`r = self.navController;

        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to
            // be looking at the root view.
            [self.navController popToRootViewControllerAnimated:NO];

            [FBSession.activeSession closeAndClearTokenInformation];

            self.viewController = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
            self.window.rootViewController = self.viewController;

            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}
- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}
Saif
  • 2,678
  • 2
  • 22
  • 38
sandy
  • 344
  • 3
  • 12
1

Possible duplicate of Facebook iOS SDK 3.0 Login Tutorial Issue with FBSession

//REPLACE
[FBSession sessionOpenWithPermissions:nil
                    completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) {
                        [self sessionStateChanged:session state:state error:error];
                    }];

//WITH
[FBSession openActiveSessionWithPermissions:nil
                               allowLoginUI:YES
                          completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                              [self sessionStateChanged:session state:state error:error];
                          }];
Community
  • 1
  • 1
Ajay
  • 1,622
  • 20
  • 36
1

It opens the facebook seesion and optionally shows the login ux

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            @"user_likes",
                            nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
ArunMak
  • 398
  • 2
  • 12
0

Just copying @Stas Zhukovskiy answer in comments to the answer box:

there is a mismatch in the docs tutorial and the sample. You should use - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI; instead. – Stas Zhukovskiy

Community
  • 1
  • 1
dmitri
  • 3,183
  • 23
  • 28
0

you can also use Sharekit It is very easy to implement and also supports other social networks. sharekit

Share kit tutorial

Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31
0

Some code of line is missing in App Delegate.Just check that one.after that u will check open session and closed session on the time of calling method for Facebook.

virantporwal
  • 989
  • 2
  • 6
  • 26