how to Use Default LoginWithFacebook Button in my code - ios

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.

Related

xcode 6: TwitterKit not keeping session open after Authentication

I'm calling up a session in my LoginViewController as seen below. The problem is that, after it authenticates and closes the popupview... the session returns nil. So it initially reaches out to the api and starts a session (the first log records nil as the session is open and the second log records "Signed in as..." with the username) then when it closes the login view it records nil again. Not exactly use what I could be doing wrong...
Could it be because I'm using TWTRSession* session to check the session? Could that be logging me off? Or is it related to me closing the popupview?
I do essentially the same thing with my facebook login but the facebook session stays open until I logout...
- (void)viewDidLoad
{
[super viewDidLoad];
self.email.delegate = self;
self.pass.delegate = self;
//self.view.frame = CGRectMake(self.bounds.size.width, self.bounds.size.height);
[Fabric with:#[[Twitter sharedInstance]]];
TWTRSession* session;
if (session) {
NSLog(#"Early bird %#", [session userName]);
[self closePopup];
}
else {
NSLog(#"No dice");
}
/*
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
// play with Twitter session
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
*/
TWTRLogInButton* logInButton = [TWTRLogInButton
buttonWithLogInCompletion:
^(TWTRSession* session, NSError* error) {
if (session) {
NSLog(#"signed in as %#", [session userName]);
[self closePopup];
} else {
NSLog(#"error: %#", [error localizedDescription]);
}
}];
I decided to include my actionsheet as well so you could see how I'm handling the login/logout situation:
- (void)sheet: (id) sender {
NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:#"email"];
UIActionSheet *sheet;
TWTRSession *session;
//if ([FBSDKAccessToken currentAccessToken]) {
if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
sheet = [[UIActionSheet alloc] initWithTitle:#"Profile"
delegate:nil // Can be another value but will be overridden when showing with handler.
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Login"
otherButtonTitles:nil];
}
else
{
sheet = [[UIActionSheet alloc] initWithTitle:#"Profile"
delegate:nil // Can be another value but will be overridden when showing with handler.
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Logout"
otherButtonTitles:#"User page", nil];
}
[sheet showInView:self.view
handler:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
if (buttonIndex == [actionSheet cancelButtonIndex]) {
NSLog(#"Cancel button index tapped");
} else if (buttonIndex == [actionSheet destructiveButtonIndex]) {
if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
//[self.view modalViewController:loginView animated:YES];
//[self presentModalViewController:loginView animated:YES];
//[self.navigationController pushViewController:loginView animated:YES];
loginView.delegate = (id) self;
[self presentPopupViewController:loginView animationType:MJPopupViewAnimationSlideTopBottom];
NSLog(#"Login View");
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:#"email"];
FBSDKLoginManager *logout = [[FBSDKLoginManager alloc] init];
[logout logOut];
}
} else {
NSLog(#"Button %i tapped", buttonIndex);
if (buttonIndex == 1) {
UserViewController * userView = [[UserViewController alloc] initWithNibName:#"UserView" bundle:[NSBundle mainBundle]];
[userView setUEmail:email];
[self presentViewController:userView animated:YES completion:nil];
}
}
}];
}
So I ended up creating my own custom IBAction to handle twitter logins. This works much better. Basically you just have to be careful with how you check if a session is active:
-(IBAction)twlogin:(id)sender
{
[Fabric with:#[[Twitter sharedInstance]]];
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
if (session) {
NSLog(#"signed in as %#", [session userName]);
TWTRShareEmailViewController* shareEmailViewController =
[[TWTRShareEmailViewController alloc]
initWithCompletion:^(NSString* email2, NSError* error) {
NSLog(#"Email %#, Error: %#", email2, error);
}];
[self presentViewController:shareEmailViewController
animated:YES
completion:nil];
[self closePopup];
NSLog(#"session check %#", [session userName]);
} else {
NSLog(#"error: %#", [error localizedDescription]);
}
}];
[[Twitter sharedInstance] logOut];
}
As you can see TWTR *session has to be included with Twitter shared instance like so:
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
I didn't do that before, and that's why it wasn't working. Hopefully this helps someone. (now if someone would help me getting Twitter emails to work... that would be greatly appreciated...)

how to ask permissions for email again if its permission is denied in authoriztion on facebook login into iOS app

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

Upload Facebook Photo And Auto Log User Out (Kiosk App)

I have an iPad app designed for use in a kiosk environment.
The user flow should be
Take Photo
Choose photo from iPad Album view
Share to Facebook and / or Twitter
Automatically log user out after image has been posted
I have the auto-logout of Twitter working properly, my issue is with the Facebook portion.
I have implemented the Graph API for internal testing, and would love to be able to post a complete story this way, but I don't think there is a way to log out from the Facebook app once the authorization and post is complete.
For a fallback, I can use the Feed Dialog and auto-logout from there, but as far as I can tell, there is no way to upload a local image for sharing to Facebook from there.
My Facebook Sharing code is as follows:
- (IBAction)facebookShare:(id)sender {
/// Package the image inside a dictionary
NSArray* image = #[#{#"url": self.mergeImages, #"user_generated": #"true"}];
// Create an object
id<FBGraphObject> object =
[FBGraphObject openGraphObjectForPostWithType:#"me/feed:photo"
title:#"a photo"
image:self.mergeImages
url:nil
description:nil];
// Create an action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
// Set image on the action
[action setObject:image forKey:#"image"];
// Link the object to the action
[action setObject:object forKey:#"photo"];
// Hardcode the location based on Facebook Place ID
id<FBGraphPlace> place = (id<FBGraphPlace>)[FBGraphObject graphObject];
[place setId:#"279163865580772"]; // Singley + Mackie
[action setPlace:place];
// Check if the Facebook app is installed and we can present the share dialog
FBOpenGraphActionShareDialogParams *params = [[FBOpenGraphActionShareDialogParams alloc] init];
params.action = action;
params.actionType = #"me/feed:share";
// If the Facebook app is installed and we can present the share dialog
if([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) {
// Show the share dialog
[FBDialogs presentShareDialogWithOpenGraphAction:action
actionType:#"photo_overlay:share"
previewPropertyName:#"photo"
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
// NSLog([NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
// Success
NSLog(#"result %#", results);
}
}];
// If the Facebook app is NOT installed and we can't present the share dialog
} else {
// Put together the Feed dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"name",
#"caption",
#"description",
#"link",
#"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
// NSLog([NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
// Auto log the user out
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"defaults fbDidLogout ........%#",defaults);
if ([defaults objectForKey:#"FBAccessTokenKey"])
{
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
[FBSession.activeSession closeAndClearTokenInformation];
}];
}
}
// A function for parsing URL parameters.
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
I have searched Stack Overflow far and wide for an answer to this, but have found no solutions.
I was finally able to figure this out! Posting the answer here to hopefully benefit others who are in the same situation.
First, add the following to your AppDelegate.m:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call) {
// Facebook SDK * App Linking *
// For simplicity, this sample will ignore the link if the session is already
// open but a more advanced app could support features like user switching.
if (call.accessTokenData) {
if ([FBSession activeSession].isOpen) {
NSLog(#"INFO: Ignoring app link because current session is open.");
}
else {
[self handleAppLink:call.accessTokenData];
}
}
}];
}
// Helper method to wrap logic for handling app links.
- (void)handleAppLink:(FBAccessTokenData *)appLinkToken {
// Initialize a new blank session instance...
FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
permissions:nil
defaultAudience:FBSessionDefaultAudienceNone
urlSchemeSuffix:nil
tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance] ];
[FBSession setActiveSession:appLinkSession];
// ... and open it from the App Link's Token.
[appLinkSession openFromAccessTokenData:appLinkToken
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Forward any errors to the FBLoginView delegate.
if (error) {
//[self.loginViewController loginView:nil handleError:error];
}
}];
}
Wherever you are calling the posting action in your app, add this line to your header file:
#property (strong, nonatomic) FBRequestConnection *requestConnection;
And the following to your implementation file:
#synthesize requestConnection;
- (IBAction)facebookShare:(id)sender {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions", #"publish_checkins", nil];
UIImage *img = self.facebookImage;
[FBSession openActiveSessionWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES
completionHandler:^(FBSession *session,FBSessionState s, NSError *error) {
[FBSession setActiveSession:session];
if (!error) {
// Now have the permission
[self processPostingImage:img WithMessage:#"Enter_your_message_here"];
} else {
// Facebook SDK * error handling *
// if the operation is not user cancelled
if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
[self presentAlertForError:error];
}
}
}];
}
-(void)logout {
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];
}
- (void)processPostingImage:(UIImage *) img WithMessage:(NSString *)message {
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error) {
// output the results of the request
[self requestCompleted:connection forFbID:#"me" result:result error:error];
};
FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me/photos" parameters:[NSDictionary dictionaryWithObjectsAndKeys:UIImageJPEGRepresentation(img, 0.7),#"source",message,#"message",#"{'value':'EVERYONE'}",#"privacy", nil] HTTPMethod:#"POST"];
[newConnection addRequest:request completionHandler:handler];
[self.requestConnection cancel];
self.requestConnection = newConnection;
[newConnection start];
}
// FBSample logic
// Report any results. Invoked once for each request we make.
- (void)requestCompleted:(FBRequestConnection *)connection
forFbID:fbID
result:(id)result
error:(NSError *)error
{
// not the completion we were looking for...
if (self.requestConnection &&
connection != self.requestConnection)
{
return;
}
// clean this up, for posterity
self.requestConnection = nil;
if (error)
{
}
else
{
[self logout];
};
}
- (void) presentAlertForError:(NSError *)error {
// Facebook SDK * error handling *
// Error handling is an important part of providing a good user experience.
// When fberrorShouldNotifyUser is YES, a fberrorUserMessage can be
// presented as a user-ready message
if (error.fberrorShouldNotifyUser) {
// The SDK has a message for the user, surface it.
[[[UIAlertView alloc] initWithTitle:#"Something Went Wrong"
message:error.fberrorUserMessage
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
} else {
}
}
Note:
To make this work with auto-logout, you also have to disable Safari on the device. This can be done by going to Settings > General > Restrictions > Allow Safari > Off. Once that is turned off, the Facebook IBAction will popup a UIWebView inside the app itself. When tapped, the current user can enter their Facebook credentials, then the app will post the image, and log the user out so the next user can use the app without having access to the previous user's Facebook details.

Permission to read user's birthday, name and email

I am trying to integrate Facebook in my app. I need the user's birthday, name and email.
I have integrated the Facebookloginview from the sdk.
If I click on log in, the app switches to Safari and the page shows that I have already authorized the app. But I cannot get the email or the birthday
If I change the permissions like user_location, and then log out and log in the app shows that it is already authorized but it is not!
If I implement a button on the xib and then add to the ibaction this code works.
AppDelegate *appDelegate [[UIApplication sharedApplication] delegate];
// If the person is authenticated, log out when the button is clicked.
// If the person is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
[appDelegate closeSession];
} else {
// The person has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES];
}
}
Why doesn't it work with the facebookloginview?
Here is my facebook loginviewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
loginview = [[FBLoginView alloc]init];
loginview.readPermissions = #[#"email"];
//Facebook start
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(sessionStateChanged:)
name:FBSessionStateChangedNotification
object:nil];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:NO];
if (!appDelegate.session.isOpen) {
// create a fresh session object
appDelegate.session = [[FBSession alloc] init];
// if we don't have a cached token, a call to open here would cause UX for login to
// occur; we don't want that to happen unless the user clicks the login button, and so
// we check here to make sure we have a token before calling open
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];
}];
}
}
}
-(void) loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.loginlabel.text = #"";
self.FBimageViewOutlet.profileID = nil;
NSLog(#"bin hier geklickt3");
}
-(void) loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
loginView.readPermissions = #[#"email"];
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
self.loginlabel.text = [NSString stringWithFormat:#"Hi, %# %# geboren am %#",my.first_name,my.last_name,my.birthday];
self.FBimageViewOutlet.profileID = my.id;
self.picView.profileID =my.id;}];
//self.loginlabel.text = [NSString stringWithFormat:#"Hi, %# %# geboren am %#",user.first_name,user.last_name,user.birthday];
self.FBimageViewOutlet.profileID = user.id;
//Hier wird gender von Facebook abgefragt
NSLog(#"%#",[user objectForKey:#"gender"]);
NSLog(#"%#",[user objectForKey:#"locale"]);
NSLog(#"%#",[user objectForKey:#"age_range"]);
NSLog(#"%#",[user objectForKey:#"username"]);
NSLog(#"%#",[user objectForKey:#"link"]);
NSLog(#"%#",[user objectForKey:#"location"]);
NSLog(#"%#",[user objectForKey:#"hometown"]);
NSLog(#"%#",[user objectForKey:#"birthday"]);
NSLog(#"%#",[user objectForKey:#"last_name"]);
NSLog(#"%#",[user objectForKey:#"first_name"]);
NSLog(#"%#",[user objectForKey:#"email"]);
NSLog(#"%#",[user objectForKey:#"user_email"]);
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject:[user objectForKey:#"gender"] forKey:#"user_gender"];
[defaults setObject:[user objectForKey:#"locale"] forKey:#"user_locale"];
[defaults setObject:[user objectForKey:#"age_range"] forKey:#"user_age_range"];
[defaults setObject:[user objectForKey:#"username"] forKey:#"user_username"];
[defaults setObject:[user objectForKey:#"link"] forKey:#"user_link"];
[defaults setObject:[user objectForKey:#"location"] forKey:#"user_location"];
[defaults setObject:[user objectForKey:#"hometown"] forKey:#"user_hometown"];
[defaults setObject:[user objectForKey:#"birthday"] forKey:#"user_birthday"];
[defaults setObject:[user objectForKey:#"last_name"] forKey:#"user_lastname"];
[defaults setObject:[user objectForKey:#"first_name"] forKey:#"user_firstname"];
}
- (void)loginView:(FBLoginView *)loginView
handleError:(NSError *)error {
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
// If the person is authenticated, log out when the button is clicked.
// If the person is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
[appDelegate closeSession];
} else {
// The person has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES];
}
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser) {
// If the SDK has a message for the user, surface it. This conveniently
// handles cases like password change or iOS6 app slider state.
alertTitle = #"Facebook Error";
alertMessage = error.fberrorUserMessage;
} else if (error.fberrorCategory == FBErrorCategoryAuthenticationReopenSession) {
// It is important to handle session closures since they can happen
// outside of the app. You can inspect the error for more context
// but this sample generically notifies the user.
alertTitle = #"Session Error";
alertMessage = #"Your current session is no longer valid. Please log in again.";
} else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
// The user has cancelled a login. You can inspect the error
// for more context. For this sample, we will simply ignore it.
NSLog(#"user cancelled login");
} else {
// For simplicity, this sample treats other errors blindly.
alertTitle = #"Unknown Error";
alertMessage = #"Error. Please try again later.";
NSLog(#"Unexpected error:%#", error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
}

Post an image on my own Facebook wall with Facebook SDK 3.0 (beta), Xcode 4.3.2

I am using Facebook SDK 3.0 (with Xcode 4.3.2 also and with ARC disabled), I already wrote the codes for login and post a text message on my own Facebook wall (codes above), everything run perfectly :
- (IBAction)loginFacebook:(id)sender {
// Initiate a Facebook instance
Facebook *facebook = [[Facebook alloc] initWithAppId:#"My_App_ID" andDelegate:nil];
// Set the session information for the Facebook instance
facebook.accessToken = self.session.accessToken;
facebook.expirationDate = self.session.expirationDate;
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I'm using Emoji-Smileys", #"name",
#"Facebook for iOS", #"caption",
#"Check out the application Emoji-Smileys for iOS to enable emoji keyboard, and make fun messages! There are 460 smileys!", #"description",
#"http://m.facebook.com/apps/hackbookios/", #"link",
#"http://a365.phobos.apple.com/us/r30/Purple/v4/fa/96/4c/fa964c2a-3a48-4f6d-7789-d3105a392a9f/mzl.evuvwyjk.png?downloadKey=1343552764_471c078e9d07fde2b00bd3f1ee1d88a7", #"picture",
nil];
// Invoke the dialog
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
- (FBSession *)createNewSession
{
self.session = [[FBSession alloc] init];
return self.session;
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
NSLog(#"User session found");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[session closeAndClearTokenInformation];
self.session = nil;
[self createNewSession];
break;
default:
break;
}
if (error) {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[errorAlert show];
}
}
- (void) openSession {
[self.session openWithCompletionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [self.session handleOpenURL:url];
}
Okay, now, I'd like to know how to post an image that I give in my application, no need to use the UIImagePickerViewController, I don't want to select the image. I don't know if I need a publish_stream, read_stream or user_likes permission or other thing.
Thanks in advance for your help.
Cheers.
You can use this code, which helped me:
[FBRequestConnection startForUploadPhoto:myImg.image
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// is success do something...
} else {
// If some error , do something...
}
}];
myImg is your image which you can replace with:
[UIImage imageNamed:#"image.jpg"]
I bet these tutorials will help you a lot:
http://www.raywenderlich.com/1488/how-to-use-facebooks-new-graph-api-from-your-iphone-app
http://www.raywenderlich.com/1578/how-to-get-a-user-profile-with-facebooks-new-graph-api-from-your-iphone-app
http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from-your-iphone-app
And bookmark this site, it has a lot of great tutorials!)

Resources