User location return null value - iphone

In my application while i retrive the user location its return a Null value, and birthdate also returns also Null value, Code as follows,
- (void)populateUserDetails {
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
self.userNameLabel.text = user.name;
self.userProfileImage.profileID = [user objectForKey:#"id"];
self.userbirthday.text=user.birthday;
self.userloc=[[user objectForKey:#"location"] objectForKey:#"name"];
}
}];
}
}
What is rong with code?Please help me to solve this issue?

For birthday and location you need to be granted with such permission. So you need to request the permission like so: Facebook SDK Documentation
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_location", // you need to have this permission
#"user_birthday", // to be approved
#"user_likes",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
And then use the FBRequest to retrieve the birthday and location. You can access username as it does not require special permission to do so.

Related

FB integration in ios app issues

I have integrated the facebook in my ios app and it is working nice.
But when i stored my FB credentials in setting it is not working.
It goes in following state
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
NSLog(#"case open session");
// if([sharePref returnFbName].length==0)
[self listFriendsFB];
break;
case FBSessionStateClosed:
NSLog(#"case FBSessionStateClosed session");
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
//[viewController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
NSLog(#"case close or fail session");
break;
default:
break;
}
It goes in FBSessionStateClosedLoginFailed , and error occurs.
And if I delete my account from phone setting it working fine i.e the session opens and it is logged in correctly.
Here is my code
- (void)openSession
{
//[self syncFacebookAccount];
NSArray *permissions=[[NSArray alloc]initWithObjects:#"email,publish_actions",nil];
dispatch_async(dispatch_get_main_queue(), ^{
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
NSLog(#"fb open session");
[self sessionStateChanged:session state:state error:error];
}];
});
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
NSLog(#"case open session");
// if([sharePref returnFbName].length==0)
[self fourthViewInitialization];
[self getFacebookInfo];
break;
case FBSessionStateClosed:
NSLog(#"case FBSessionStateClosed session");
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
//[viewController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
NSLog(#"case close or fail session");
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:AlertTitle
message:#"We are currently facing facebook connectivity issues. Please try again later!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
NSLog(#"error facebook : %#",error);
[alertView show];
[self hideActivityIndicator];
}
}
#pragma facebook methods
-(void)getFacebookInfo
{
// [NSThread detachNewThreadSelector:#selector(startLoader) toTarget:self withObject:nil];
NSString *query =[NSString stringWithFormat:#"Select name,uid,pic_square from user where uid=me()"];
// Set up the query parameter
my_info=[[NSMutableDictionary alloc]init];
my_info_array=[[NSMutableArray alloc ]init];
NSDictionary *queryParam =
[NSDictionary dictionaryWithObjectsAndKeys:query, #"q", nil];
// Make the API request that uses FQL
[FBRequestConnection startWithGraphPath:#"/fql"
parameters:queryParam
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", [error localizedDescription]);
[self hideActivityIndicator];
} else {
// NSLog(#"Result: %#", result);
// [self hideActivityIndicator];
my_info_array=(NSMutableArray *)[result objectForKey:#"data"];
my_info=[my_info_array objectAtIndex:0];
//NSLog(#"my info array %#",my_info_array);
NSLog(#"my info from fb %#",my_info);
//return my_info;
[firstView removeFromSuperview];
[self.view addSubview:fourthView];
displayNameLabel.text=[NSString stringWithFormat:#"%#%#",displayNameLabel.text,[my_info objectForKey:#"name"]];
[registerRequest setObject:[my_info objectForKey:#"name"] forKey:#"displayName"];
//[registerRequest setObject:[my_info objectForKey:#"email"] forKey:#"emailId"];
[registerRequest setObject:[my_info objectForKey:#"uid"] forKey:#"uid"];
[SharedPreferenceClass setFBUID:[my_info objectForKey:#"uid"]];// 30 sept
[registerRequest setObject:[my_info objectForKey:#"pic_square"] forKey:#"profilePic"];
NSString* picPath = [my_info objectForKey:#"pic_square"];
if (picPath.length>0) {
[SharedPreferenceClass setUserProfilePictureDefault:picPath];
}else{
[SharedPreferenceClass setUserProfilePictureDefault:#""];
}
//displayName.text=[my_info objectForKey:#"name"];
[self hideActivityIndicator];
}
}];
}
Thanks in advance.
I used this to open session when FB credentials are available in phone settings
// Initialize a session object
FBSession *session = [[FBSession alloc] init];
// Set the active session
[FBSession setActiveSession:session];
[session openWithBehavior:FBSessionLoginBehaviorWithFallbackToWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// Respond to session state changes,
NSLog(#"fb open session %#",error);
[self sessionStateChanged:session state:status error:error];
}];
And once session is opened I have used following code to get FB friends data.
NSArray *permissions=[[NSArray alloc]initWithObjects:#"email,publish_actions",nil];
dispatch_async(dispatch_get_main_queue(), ^{
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
NSLog(#"fb open session");
[self sessionStateChanged:session state:state error:error];
}];
});

Fetching user details from Facebook in iOS

I'm new to iPhone development. I'm doing an App where the user has 2 login through Facebook, once he submits his credentials and clicks on Sign In button, I have to fetch details like First name, email and gender from Facebook and after fetching the user has to be directed to the Registration page of the app with the details filled and i need this app to be compatible for iPhone 4,4s and 5.
I tried doing this using the Facebook Graph API but couldn't get it, so anyone can help me out.
Thanks in Advance.
You can do this by using following code :
[FBSession openActiveSessionWithReadPermissions:#[#"email",#"user_location",#"user_birthday",#"user_hometown"]
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
switch (state) {
case FBSessionStateOpen:
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
NSLog(#"error:%#",error);
}
else
{
// retrive user's details at here as shown below
NSLog(#"FB user first name:%#",user.first_name);
NSLog(#"FB user last name:%#",user.last_name);
NSLog(#"FB user birthday:%#",user.birthday);
NSLog(#"FB user location:%#",user.location);
NSLog(#"FB user username:%#",user.username);
NSLog(#"FB user gender:%#",[user objectForKey:#"gender"]);
NSLog(#"email id:%#",[user objectForKey:#"email"]);
NSLog(#"location:%#", [NSString stringWithFormat:#"Location: %#\n\n",
user.location[#"name"]]);
}
}];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
} ];
and don't forgot to import FacebookSDK/FacebookSDK.h in your code.
EDIT : Update for Facebook SDK v4 (23 April,2015)
Now, Faceboook have released new SDK with major changes. In which FBSession class is deprecated. So all users are suggested to migrate to new sdk and APIs.
Below I have mentioned, how we can get user details via Facebook SDK v4 :
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#”fetched user:%#”, result);
}
}];
}
But before fetching user details, we have to integrate new Facebook login in our code as described in Documentation here.
Here is the Changelog for SDK v4. I suggest going through it for being updated.
Add info.plist file:
- (IBAction)btn_fb:(id)sender
{
if (!FBSession.activeSession.isOpen)
{
NSArray *_fbPermissions = #[#"email",#"publish_actions",#"public_profile",#"user_hometown",#"user_birthday",#"user_about_me",#"user_friends",#"user_photos",];
[FBSession openActiveSessionWithReadPermissions:_fbPermissions allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
{
if (error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else if(session.isOpen)
{
[self btn_fb:sender];
}
}];
return;
}
[FBRequestConnection startWithGraphPath:#"me" parameters:[NSDictionary dictionaryWithObject:#"cover,picture.type(large),id,name,first_name,last_name,gender,birthday,email,location,hometown,bio,photos" forKey:#"fields"] HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
{
if (!error)
{
if ([result isKindOfClass:[NSDictionary class]])
{
NSLog(#"%#",result);
}
}
}
}];
}
Please refer these following links, sothat you can get some idea.
https://www.parse.com/tutorials/integrating-facebook-in-ios
Facebook iOS 6 - get user info
get Facebook user profile data after getting access token in iOS 5
Display a user's profile name and image through the Facebook instance for iOS
How to cache Facebook User Info with Facebook Login in IOS App
How do I get the full User object using Facebook Graph API and facebook ios sdk?
Hope it will helps you....
Use the FBConnect API for fetch user information.its easy to use
Fbconnect API
Hope it Works For you :)
you have to use graph path to get user Information.
-(void)getEmailId
{
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
- (void)openSession
{
if (internetActive) {
NSArray *permissions=[NSArray arrayWithObjects:#"read_stream",#"email",nil];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"" message:#"Internet Not Connected" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}
#pragma mark- request delegate methods
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"request did load successfully....");
// __block NSDictionary *dictionary=[[NSDictionary alloc] init];
if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary* json = result;
NSLog(#"email id is %#",[json valueForKey:#"email"]);
NSLog(#"json is %#",json);
[[NSUserDefaults standardUserDefaults] setValue:[json valueForKey:#"email"] forKey:#"fbemail"];
[self.viewController login:YES];
}
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"" message:#"Server not responding.." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
[self fblogout];
[self showLoginView];
NSLog(#"request did fail with error");
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
//state open action
}
// Initiate a Facebook instance
facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
facebook.accessToken = FBSession.activeSession.accessToken;
facebook.expirationDate = FBSession.activeSession.expirationDate;
if (![[NSUserDefaults standardUserDefaults] valueForKey:#"fbemail"]) {
[MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES];
[self getEmailId];
}
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];
facebook = nil;
[self showLoginView];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([error.userInfo[FBErrorParsedJSONResponseKey][#"body"][#"error"][#"type"] isEqualToString:#"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(#"The facebook session was invalidated");
[self logoutButtonTouchHandler:nil];
} else {
NSLog(#"Some other error: %#", error);
}
}];
Facebook has currently updated their SDK version to 4.x. To Fetch user's profile information you will need to explicitly call graph API after login success (ask for required permissions).
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me"
parameters:#{ #"fields": #"id,name,email"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
}];

Facebook sdk 3 returns error code 5

I'm using the new facebook SDK... Here I want to post a image from my application to app.
This is the code
NSData* imageData = UIImagePNGRepresentation(emailImage);
NSLog(#"%#",emailImage);
if (xapp.session.isOpen) {
[xapp.session closeAndClearTokenInformation];
} else {
if (xapp.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
xapp.session = [[FBSession alloc] init];
}
[xapp.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
}];
}
self.postParams =[[NSMutableDictionary alloc] initWithObjectsAndKeys:imageData, #"source",#"My Art", #"message",nil];
// No permissions found in session, ask for it
if ([[FBSession activeSession]isOpen]) {
if ([[[FBSession activeSession]permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
[[FBSession activeSession] reauthorizeWithPublishPermissions:[NSArray arrayWithObjects:#"publish_actions",#"publish_stream",nil]
defaultAudience:FBSessionDefaultAudienceOnlyMe
completionHandler:^(FBSession *session, NSError *error) {
[self publishStory];
}];
}else{
[self publishStory];
}
}else{
NSLog(#"Sessionisclosed");
/
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:#"publish_stream",#"publish_actions",nil]
defaultAudience:FBSessionDefaultAudienceOnlyMe
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
[FBSession setActiveSession:session];
[self publishStory];
}else{
NSLog(#"error:%d",[error code]);
}
}];
}
- (void)publishStory
{
NSLog(#"PublishStory%#",self.postParams);
[FBRequestConnection
startWithGraphPath:#"me/feed"
parameters:self.postParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
#"Posted action, id: %#",
[result objectForKey:#"id"]];
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:nil
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}];
NSLog(#"PublishStory");
}
Here I'm getting error code 5,which says that there is some permissions problems while posting the image ... But I have used both publish_stream and publish_actions ..I don't know what I'm missing ......
You need to switch to feed dialog because the opengraph is deprecated since feb 2013
refer the link for feeddialog
http://developers.facebook.com/docs/reference/dialogs/feed/

Facebook Wall Post just after Facebook Signup iOS- facebook SDK 3.2

Facebook SDK 3.2
I need to post a wall post on facebook just after sign-in with faceboook.
Here is the Code i am using:
Here i have a opened the Session For Facebook:
- (IBAction)FBSignup:(id)sender {
[[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
On State Change, I am populating Data with [self populateUserDetails] and then trying to post on facebook wall as well with [self postOnFB] but there is no post on facebook wall.
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
[self populateUserDetails];
[self postOnFB];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:SCSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Here i am populting data with the facebook user object where i need to.
- (void)populateUserDetails
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
///////////////////////////////
// Populating Data Where Needed
///////////////////////////////
// [self postOnFB];
}
}];
}
}
Here i try to post the wall post with [self postOnFB] it gives error:
Terminating app due to uncaught exception
'com.facebook.sdk:InvalidOperationException', reason: 'FBSession: It
is not valid to reauthorize while a previous reauthorize call has not
yet completed.
Here i am posting to the wall.
- (void)postOnFB{
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
[self fbPostSettings];
}
}];
} else {
// If permissions present, publish the story
[self fbPostSettings];
}
}
setting parameters for facebook wall post:
- (void)fbPostSettings{
NSDictionary * facebookParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
kFacebookPostLink, #"link",
kFacebookPostImage, #"picture",
kFacebookPostName, #"name",
kFacebookPostCaption, #"caption",
kFacebookPostDescription, #"description",
nil];
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:facebookParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
#"Posted action, id: %#",
[result objectForKey:#"id"]];
}
NSLog(#"%#",alertText);
}];
}
- (void)sessionStateChanged:(NSNotification*)notification {
[self populateUserDetails];
}
If i call postOnFB on some button action selector (after completion of data population through facebook user object), it post fine on the wall. But i need to post it just after i get the user object in startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary *user, NSError *error) method.
Please help. Thank you everyone :)
Instead of opening session and then requesting publish permissions, directly ask for publish permissions to open the session
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:#"publish_stream",
nil] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session,
FBSessionState state, NSError *error) {
if (FBSession.activeSession.isOpen && !error) {
[self fbPostSettings];
}];

New Facebook SDK FBSession sessionOpenWithPermissions

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 ?
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
}];
}
}
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];
}];
}
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];
}];
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];
}];}
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
you can also use Sharekit
It is very easy to implement and also supports other social networks.
sharekit
Share kit tutorial
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.