Trouble verifying Facebook publish_actions permission - ios

I have successfully logged into Facebook and asked for publish_actions permission. The permission is granted, as I can readily verify on https://www.facebook.com/settings?tab=applications and also by calling
-(void) whatPermissionsDoIHave
{
[FBRequestConnection startWithGraphPath:#"/me/permissions"
completionHandler:^(
FBRequestConnection *connection,
NSDictionary* result,
NSError *error
) {
//do checks in here
}
}];
}
So I know for certain that I have permission. However, whenever it's time to share, my permission check returns false. I will now show my permission check
+ (BOOL)hasWritePermissions
{
if (!FBSession.activeSession.isOpen) return NO;
NSLog(#"So the session is at least open");
return [FBSession.activeSession.permissions indexOfObject:#[#"publish_actions"]] != NSNotFound;
}
So am I missing something else? Perhaps something I should have done in AppDelegate or such? Thanks for any insight.
Right now the only call I am making in AppDelegate is
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(#"In fallback handler");
}];
}

The permissions property is an array of strings, but you're calling indexOfObject on another array, which is probably why it can't find it. Try this instead:
return [FBSession.activeSession.permissions indexOfObject:#"publish_actions"] != NSNotFound;

Related

'user' is always nil in logInInBackgroundWithReadPermissions in Parse iOS SDK

I'm using Facebook SDK with Parse SDK in user Login.
Using 'logInInBackgroundWithReadPermissions' to perform login, Even though user is authenticated the app, 'user' is always nil in the response. And error is:
Error Domain=com.facebook.sdk.login Code=304 "The operation couldn’t
be completed. (com.facebook.sdk.login error 304.)"
Here is my Implementation:
• Basic configuration:
I have configured every thing in .plist
and followed guidelines at link1, link2
• Imported frameworks:
• Implementation in Appdelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[Parse setApplicationId:#"I have used real key here" clientKey:#"I have used real key here"];
[PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions];
return YES; }
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
• Login Implementation:
NSArray *fbPermissions = [[NSArray alloc] initWithObjects: #"public_profile", nil];
[PFFacebookUtils logInInBackgroundWithReadPermissions:fbPermissions block:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
}
else if (user.isNew) {
NSLog(#"User signed up and logged in through Facebook!");
} else {
NSLog(#"User logged in through Facebook!");
}
}];
But in the call back, 'user' is always nil. Did anyone face the same issue?
- Thanks in advance.
I was not able to find out cause for the issue. But I just deleted all frameworks related to Parse and Facebook and Re-added all of them. It worked fine. So, there is no issue in the code.
in AppDelegate.m file, under didFinishLaunchingWithOptions method, initialize Parse before initializing PFFacebookUtils.

Parse: [PFFacebookUtils reauthorizeUser:] callback never gets called

I'm using the Parse framework for social authentication. My app has a really simple method that requests post permission on Facebook.
My problem: The block that I pass into reauthorizeUser: never gets called!
+(void)acquirePublishPermissionsForUser:(DHUser *)user WithCompletion:(void (^) (BOOL succeeded, NSError *error))completion
{
[PFFacebookUtils reauthorizeUser:user
withPublishPermissions:#[#"publish_actions"]
audience:FBSessionDefaultAudienceFriends
block:^(BOOL succeeded, NSError *error) {
completion(succeeded, error);
}];
}
This method works fine. In this case, the Facebook app opens up and prompts for permission. Upon hitting OK, you're returned to my app. Both openURL & applicationDidBecomeActive get called.
This is in my app delegate:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// Handle Facebook authentication
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:[PFFacebookUtils session]];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Handle Facebook Authentication
[FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
}
I've set a breakpoint to verify: my block never gets executed. It's really important, because I want to post to Facebook after I get permission. Otherwise it's kinda useless.
Any ideas?
I ended up using a straight FB-API call ([[FBSession activeSession] requestNewPublishPermissions]).
It does weird stuff with my session and auth token data, but at least the callback gets executed. Definitely not a real solution but it'll do.

Facebook SDK on iOS App Session Fixing

I'm trying to authenticate users through facebook. I've read the SDK and done every necessary things that need to be done, BUT if I try to Login into my app while LoggedIn in this place,
It Tells me SESSION CLOSED but if I logged out and try to SIgnIn into my app, it checks if I've the facebook app and try to check if logged in if I'm, it updates and do what I want it to do, but if I'm not, it brings a Dialog for me to login. But, once I sign into the Image above, the other thing happens CLOSED SESSION ERROR. Would appreciate the help, thanks.
My Code Below
//For the button
- (IBAction)facebook:(id)sender {
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
[FBSession.activeSession closeAndClearTokenInformation];
} else {
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile",#"user_friends"]
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
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];
if (state == FBSessionStateOpen) {
[self fetchUserDetails];
UINavigationController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
}];
}
}
//AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
//return [FBAppCall handleOpenURL:url sourceApplication:annotation];
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Handle the user leaving the app while the Facebook login dialog is being shown
// For example: when the user presses the iOS "home" button while the login dialog is active
[FBAppCall handleDidBecomeActive];
}
// 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");
// Show the user the logged-in UI
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(#"Session closed");
// Show the user the logged-out UI
}
// 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];
NSLog(#"%#", alertText);
} 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];
NSLog(#"%#", alertText);
// 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];
NSLog(#"%#", alertText);
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
}
}
I add a similar problem, in my case I found the reason, I was using an application id that was not correct (because of test environment/production environment configuration of my app, I was developing and I had no facebook app for the development application id).
I'm sharing my approach for debugging the problem:
just launch the app from xcode, put a break point where the error is intercepted (CordovaFacebook.m) and there if you are lucky you should see a better description of the problem
Furthermore, remember to set the correct fb app id inside .plist
remember also that if you are using a test application on facebook, that application should be either publicly available or you should set some test users inside the ROLES tab of the admin section
Another source of interesting debugging informations is the following:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
if (!url) {
return NO;
}
set a break point there and inspect the url variable content.

Facebook Integration and data fetching not working

I have used the following code to fetch data from the Facebook to my app.
I have did all the other stuff in app delegate and plist.
This sometimes works and fetched data and sometimes don't. Can anyone help me with this. I can't figure what the problem is!
- (IBAction)facebook:(id)sender
{
FBLoginView *loginView=[[FBLoginView alloc]init];
loginView.delegate=self;
loginView.readPermissions = #[#"first_name",
#"last_name",
#"location",
#"id",
#"access_token"];
NSArray* permissions = [NSArray arrayWithObjects: #"email", nil];
loginView.readPermissions=permissions;
[FBSession openActiveSessionWithAllowLoginUI:YES];
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user1,
NSError *error) {
if (!error) {
firstname=user1.first_name;
lastname=user1.last_name;
city=[user1.location objectForKey:#"name"];
email=user1[#"email"];
fbid=user1.id;
Loggedin=#"Y";
[[NSUserDefaults standardUserDefaults]setObject:Loggedin forKey:#"token"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
if (![fbid isEqualToString:#""])
{
[self performSegueWithIdentifier: #"Facebooksegue" sender: self];
}
NSLog(#"%#",firstname);
NSLog(#"%#",lastname);
NSLog(#"%#",city);
NSLog(#"%#",email);
NSLog(#"%#",fbid);
}];
}
My AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FBSession class];
return YES;
}
I had a similar situation and I had used almost the same code as you did.
My problem was with the test Facebook id which I had used. I hadn't had it verified so it didn't respond with the email. Try using another account and it might work. If not check whether your app is authorized with the Facebook account synchronized with your account in the settings.
Hope this helps.
Go to the blue project icon at the top left, select your target, go to build settings, find other linker flags, add -ObjC. This will make it so that the entire framework gets statically linked and thus you can reference FBLoginView in the storyboard successfully.

FBSessionStateClosedLoginFailed every time I try and log into my app

Hi this returns a state of FBSessionStateClosedLoginFailed every time I run it. I've checked my bundle name, URL setting etc and all cool. In fact face book knows i've authenticated against the app. However ever time I call openWithCompletion my session fails !!
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
switch (status) {
case FBSessionStateOpen:
NSLog (#"Open");
break;
case FBSessionStateClosed:
NSLog (#"Closed");
break;
case FBSessionStateClosedLoginFailed:
NSLog (#"Failed");
break;
default:
break;
}
[self updateView];
}];
I have created this in the app delegate that also get called successfully, but always returns false:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
Did you try this in your app delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [FBSession.activeSession handleOpenURL:url];
}
I had a similar issue got it fixed here
OK how weird. I took the FB SDK login example used my FB app code and bundle ID and it worked. I then reran mine and it worked ? Maybe FB server replication took a bit of time ? No idea but it goes now ....

Resources