I would like to make "Facebook Log-in" function in iOS. The problem is [FBSession alloc] is not working.
[FBSession alloc] has 0X0000000.
so [[FBSession alloc] init] has crash. Why [FBSession alloc] has 0X00000?
Code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateView];
SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
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
[self updateView];
}];
}
}
}
Related
I have a problem with the Facebook SDK in iOS. Everything works perfectly but I have a problem with the login flow. When the user hits the "log into Facebook" button he gets directed to Facebook to allow my app to get permissions. That's fine. But when iOS is changing from the browser to my app and trying to make a request I get this error message:
FBSDKLog: Error for request to endpoint 'search?q=concert&type=event&limit=10': An open FBSession must be specified for calls to this endpoint.
When I'm closing the app and start it again and go and execute my request it works without error. I get my reqeust. That´s my code I know its not very neat.
ViewdidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
//Some other Code
if ([[FBSession activeSession]state])
{
NSLog(#"User is logged in");
[loginView setHidden:TRUE];
} else {
// try to open session with existing valid token
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_events",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
if([FBSession openActiveSessionWithAllowLoginUI:NO]) {
} else {
NSLog(#"User is logged out");
//Create the FB Login-Button
loginView = [[FBLoginView alloc] initWithReadPermissions:#[#"user_events"]];
// Align the button in the center horizontally
loginView.frame = CGRectOffset(loginView.frame, (self.view.center.x - (loginView.frame.size.width / 2)), 200);
[self.view addSubview:loginView];
}
}
[self getEvents];
}
getEvents:
-(void)getEvents{
//Get General Events
[FBRequestConnection startWithGraphPath:#"search?q=concert&type=event&limit=10"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
//Some Code
else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
}
appDidEnterForeground
- (void)appDidEnterForeground:(NSNotification *)notification {
NSLog(#"App did enter forground again.");
[self viewDidLoad];
}
ok, well I solved my own problem: I read through the docs again extremely carefully did everything again, added this to my delegate:
// Whenever a person opens the app, check for a cached session
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// If there's one, just open the session silently, without showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile"]
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
// Handler for session state changes
// This method will be called EACH time the session state changes,
// also for intermediate states and NOT just when the session open
[self sessionStateChanged:session state:state error:error];
}];
}
}
// This method will handle ALL the session state changes in the app
-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error: (NSError *)error
{
// If the session was opened successfully
// customize your code...
}
and then I´m executing my method, which contains my request like this
// Logged-in user experience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
NSLog(#"You're logged in. (Method: loginviewshowingloggedinuser");
//If the User is logged in we can fetch the events
[self getEvents];
}
In my app,there is facebook login am creating custom ui for facebook login.I am fetching users public profile and email.On first time logining in with facebook there is an authorization screen shown as shown in 1.jpg.In this screen there is a option to user which displays edit the info you provide.On clicking that edit button user is directed to next screen where he can deny access for email.My issue is if there is a provision that edit the info you provide button is hidden or is there any possibilty that user is asked for email permission again.
1.jpg
My code is below:
*********Appdelegate.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSString *strBasePath;
-(void)openActiveSessionWithPermissions:(NSArray *)permissions allowLoginUI:(BOOL)allowLoginUI;
#end
******Appdelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
-(void)openActiveSessionWithPermissions:(NSArray *)permissions allowLoginUI:(BOOL)allowLoginUI{
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Create a NSDictionary object and set the parameter values.
NSDictionary *sessionStateInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
session, #"session",
[NSNumber numberWithInteger:status], #"state",
error, #"error",
nil];
// Create a new notification, add the sessionStateInfo dictionary to it and post it.
[[NSNotificationCenter defaultCenter] postNotificationName:#"SessionStateChangeNotification"
object:nil
userInfo:sessionStateInfo];
}];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded) {
[self openActiveSessionWithPermissions:nil allowLoginUI:NO];
}
[FBAppCall handleDidBecomeActive];
}
#import "ViewController.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import <FacebookSDK/FacebookSDK.h>
#interface ViewController ()
#property (nonatomic, strong) AppDelegate *appDelegate;
-(void)hideUserInfo:(BOOL)shouldHide;
-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.imgProfilePicture.layer.masksToBounds = YES;
self.imgProfilePicture.layer.cornerRadius = 30.0;
self.imgProfilePicture.layer.borderColor = [UIColor whiteColor].CGColor;
self.imgProfilePicture.layer.borderWidth = 1.0;
[self hideUserInfo:YES];
self.activityIndicator.hidden = YES;
self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleFBSessionStateChangeWithNotification:) name:#"SessionStateChangeNotification" object:nil];
}
-(void)hideUserInfo:(BOOL)shouldHide{
self.imgProfilePicture.hidden = shouldHide;
self.lblFullname.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
}
- (IBAction)toggleLoginState:(id)sender {
if ([FBSession activeSession].state != FBSessionStateOpen &&
[FBSession activeSession].state != FBSessionStateOpenTokenExtended) {
[self.appDelegate openActiveSessionWithPermissions:#[#"public_profile", #"email"] allowLoginUI:YES];
}
else{
// Close an existing session.
[[FBSession activeSession] closeAndClearTokenInformation];
// Update the UI.
[self hideUserInfo:YES];
self.lblStatus.hidden = NO;
self.lblStatus.text = #"You are not logged in.";
}
}
-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification{
// Get the session, state and error values from the notification's userInfo dictionary.
NSDictionary *userInfo = [notification userInfo];
FBSessionState sessionState = [[userInfo objectForKey:#"state"] integerValue];
NSError *error = [userInfo objectForKey:#"error"];
self.lblStatus.text = #"Logging you in...";
[self.activityIndicator startAnimating];
self.activityIndicator.hidden = NO;
// Handle the session state.
// Usually, the only interesting states are the opened session, the closed session and the failed login.
if (!error) {
// In case that there's not any error, then check if the session opened or closed.
if (sessionState == FBSessionStateOpen) {
// The session is open. Get the user information and update the UI.
[FBRequestConnection startWithGraphPath:#"me"
parameters:#{#"fields": #"first_name, last_name, picture.type(normal), email"}
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Set the use full name.
self.lblFullname.text = [NSString stringWithFormat:#"%# %#",
[result objectForKey:#"first_name"],
[result objectForKey:#"last_name"]
];
// Set the e-mail address.
self.lblEmail.text = [result objectForKey:#"email"];
// Get the user's profile picture.
NSURL *pictureURL = [NSURL URLWithString:[[[result objectForKey:#"picture"] objectForKey:#"data"] objectForKey:#"url"]];
self.imgProfilePicture.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:pictureURL]];
strFbEmail=[result objectForKey:#"email"];
strFbFirstName=[result objectForKey:#"first_name"];
strFbLastName=[result objectForKey:#"last_name"];
strFbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
NSLog(#"%# -------- %# -------- %# -------%#",strFbAccessToken,strFbEmail,strFbFirstName,strFbLastName);
//[self sendFbData];
// Make the user info visible.
[self hideUserInfo:NO];
// Stop the activity indicator from animating and hide the status label.
self.lblStatus.hidden = YES;
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}
else{
NSLog(#"%#", [error localizedDescription]);
}
}];
[self.btnToggleLoginState setTitle:#"Logout" forState:UIControlStateNormal];
}
else if (sessionState == FBSessionStateClosed || sessionState == FBSessionStateClosedLoginFailed){
// A session was closed or the login was failed. Update the UI accordingly.
[self.btnToggleLoginState setTitle:#"Login" forState:UIControlStateNormal];
self.lblStatus.text = #"You are not logged in.";
self.activityIndicator.hidden = YES;
}
}
else{
// In case an error has occurred, then just log the error and update the UI accordingly.
NSLog(#"Error: %#", [error localizedDescription]);
[self hideUserInfo:YES];
[self.btnToggleLoginState setTitle:#"Login" forState:UIControlStateNormal];
}
}
if ([FBSession.activeSession.permissions indexOfObject:#"email"] == NSNotFound) {
[FBSession.activeSession requestNewReadPermissions:#[#"email"]
completionHandler:^(FBSession *session,
NSError *error)
{
// Handle new permissions callback
}];
} else {
// permission exists
}
Try this. Hope it helps
I am new to IOS and I created a Facebook custom login and logout buttons for my app using Facebook SDK. Login button works as I expected, but when I clicked my logout button, it gives the login page as I redirected to it. Then, when I try to login again, it doesn't show the Facebook login UI. But when I logged out my Facebook app and try to login to my app, the login UI works well. Here is my logout button.
- (IBAction)btnClicked:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
NSLog(#"log out");
LoginUIViewController *loginController1= [[LoginUIViewController alloc] initWithNibName:#"LoginUIViewController" bundle:nil];
[self presentViewController:loginController1 animated:YES completion:nil];
}
Do I need to implement a logout button in my view controller or does FB SDK provides the logout facility it self?
# Vinod Jat,,
I made following changes in my appDelegate..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[SplashViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Whenever a person opens the app, check for a cached session
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// If there's one, just open the session silently, without showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile"]
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
return YES;
}
//This method will handle ALL the session state changes in the app
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(#"Session opened");
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(#"Session closed");
}
.........
}
**In my loginUIViewController I have added following method..**
- (IBAction)FbloginButton:(id)sender {
NSLog(#"inside login button");
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
NSLog(#"inside login button2");
// Retrieve the app delegate
TNLAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error:error];
NSLog(#"inside login button3");
}];
//redirect to now playing page
NowPlayingViewController *nowPlaying= [[NowPlayingViewController alloc] initWithNibName:#"NowPlayingViewController" bundle:nil];
[self presentViewController:nowPlaying animated:YES completion:nil];
}
here is my code which is running properly but I want to use LoginWithFacebook default button which is provided by facebook.
Is there any image provided by facebook then please give me suggestion.
thankx in advance.....
#import "FacebbokViewController.h"
#import "UserAppAppDelegate.h"
#interface FacebbokViewController ()
#end
#implementation FacebbokViewController
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// button which I have used
UIButton *login=[[UIButton alloc]initWithFrame:CGRectMake(100,100, 200,80)];
[login setBackgroundColor:[UIColor blueColor]];
[login setTitle:#"login With facebook" forState:UIControlStateNormal];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[login addTarget:self action:#selector(loginWithFacebook) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:login];
}
// method which excute on my button click
-(IBAction)loginWithFacebook{
UserAppAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"email", nil];
if(!appDelegate.session.isOpen)
{
// create a fresh session object
appDelegate.session = [[FBSession alloc] init];
[FBSession setActiveSession: appDelegate.session];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
else{
NSLog(#"hi");
}
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(#"Session opened");
[self userData]; // method created to fetch user’s data.
// Show the user the logged-in UI
// do all the things as you have the info you requested from facebook
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(#"Session closed");
// Show the user the logged-out UI
[FBSession.activeSession closeAndClearTokenInformation];
}
// Handle errors
if (error){
NSLog(#"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = #"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
[self showMessage:alertText withTitle:alertTitle];
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(#"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = #"Session Error";
alertText = #"Your current session is no longer valid. Please log in again.";
[self showMessage:alertText withTitle:alertTitle];
// Here we will handle all other errors with a generic error message.
// We recommend you check our Handling Errors guide for more information
// https://developers.facebook.com/docs/ios/errors/
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:#"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:#"body"] objectForKey:#"error"];
// Show the user an error message
alertTitle = #"Something went wrong";
alertText = [NSString stringWithFormat:#"Please retry. \n\n If the problem persists contact us and mention this error code: %#", [errorInformation objectForKey:#"message"]];
[self showMessage:alertText withTitle:alertTitle];
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
}
}
-(void)showMessage:(NSString*)alertMessage withTitle:(NSString*)alertTitle
{
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
-(void)userData
{
// Start the facebook request
[FBRequestConnection startWithGraphPath:#"me" parameters:[NSDictionary dictionaryWithObject:#"id,email" forKey:#"fields"] HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error)
{
//if(!error){
// NSLog(#"result %#",result);
NSString *fbid =result[#"email"];
NSLog(#"result %#",fbid);
//}
}];
}
you can use FBLoginView to have the the functionality as your requirement , you can use like this
FBLoginView *loginView = [[FBLoginView alloc] init];
loginView.frame = YOURFRAME;
[self.view addSubview:loginView];
Take a look at FBLoginView in the Facebook SDK. Official link/tutorial. In the later part of the tutorial, implementing custom views and logging in with API calls is also covered. But note that you would have to design your button on your own in the latter case.
I'm using facebook sdk 3.0.8 for ios. when i try to login with facebook it works fine but sometimes when i try to login after logging out then app is being crashed.
here is exception message
*** Assertion failure in -[FBSession close], /Users/jacl/src/ship/ios-sdk/src/FBSession.m:342
can you please tell me where i'm going wrong?
Here is code inside AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self.session close];
}
#pragma mark Template generated code
// FBSample logic
// It is possible for the user to switch back to your application, from the native Facebook application,
// when the user is part-way through a login; You can check for the FBSessionStateCreatedOpenening
// state in applicationDidBecomeActive, to identify this situation and close the session; a more sophisticated
// application may choose to notify the user that they switched away from the Facebook application without
// completely logging in
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
// FBSample logic
// this means the user switched back to this app without completing a login in Safari/Facebook App
if (self.session.state == FBSessionStateCreatedOpening) {
// BUG: for the iOS 6 preview we comment this line out to compensate for a race-condition in our
// state transition handling for integrated Facebook Login; production code should close a
// session in the opening state on transition back to the application; this line will again be
// active in the next production rev
//[self.session close]; // so we close our session and start over
}
}
Code Inside View Controller
-(IBAction)connectWithFacebook{
DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate];
if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
{
appDelegate.session = [[FBSession alloc] init];
}
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
#"email",
nil];
//app crashes here
[FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (session.isOpen)
{
NSLog(#"LoginVC->Session is open");
appDelegate.session=session;
[userDefaults setObject:appDelegate.session.accessToken forKey:#"facebook_token"];
[userDefaults setObject:paramFBId forKey:#"facebook_id"];
}
else
{
NSLog(#"LoginVC->Session is not open");
}
}//end completionHandler
];
}
-(IBAction)logout:(id)sender{
DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen) {
[appDelegate.session closeAndClearTokenInformation];
[[NSUserDefaults userDefaults] removeObjectForKey:#"facebook_id"];
[[NSUserDefaults userDefaults] removeObjectForKey:#"facebook_token"];
}
}
Edit:
removed the following code and now it works fine
if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
{
appDelegate.session = [[FBSession alloc] init];
}
here is updated code
-(IBAction)connectWithFacebook{
if ([InternetChecker isConnected])
{
DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate];
/* Removed following if block
if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
{
appDelegate.session = [[FBSession alloc] init];
}
*/
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
#"email",
nil];
[FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (session.isOpen)
{
NSLog(#"LoginVC->Session is open");
appDelegate.session=session;
[userDefaults setObject:appDelegate.session.accessToken forKey:#"facebook_token"];
[userDefaults setObject:paramFBId forKey:#"facebook_id"];
}
else
{
NSLog(#"LoginVC->Session is not open);
}
}//end completionHandler
];
}
}
Looking at FBSession.m, the assertion in close is as follows:
NSAssert(self.affinitizedThread == [NSThread currentThread], #"FBSession: should only be used from a single thread");
Are you calling -close from a thread other than the one you created the session on?
Looking into this further. You're misusing the API. You are creating
appDelegate.session = [[FBSession alloc] init];
but then you are calling
[FBSession openActiveSessionWithPermissions ...
which creates an entirely new session. Meaning you never opened appDelegate.session, and therefore you should not try to close it. What you should be doing instead is the following:
[FBSession openActiveSessionWithPermissions ...
appDelegate.session = [FBSession activeSession];
Another option is to just do:
appDelegate.session = [[FBSession alloc] init];
[appDelegate.session openWithCompletionHandler: ...
You should perform your code on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// do something
}];
});