0

I'm trying to to use the iOS Facebook-SDK correctly and I tried every samples of Facebook and also on the web and idk what im doing wrong.

What I would like to achieve is to ask for the Facebook permission only if a request is going to be made.

Currently the only solution that is "working" without to have problems with the Session is to ask for permission directly on app launch. Thats not really what I would like to go with.

So many Apps uses that what I would like to have. Maybe cause they dont use the system facebook permission? They go to the Safari and switch back. Anyhelp is appreciated!

Here's my actual code:

// ****************************************************************************
// App switching methods to support Facebook Single Sign-On.
// ****************************************************************************
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    // Facebook SDK * login flow *
    // Attempt to handle URLs to complete any auth (e.g., SSO) flow.
    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...
    //Open session
    NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];
    FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
                                                     permissions:permissionsArray
                                                 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) {
                                  NSLog(@"%@", error);
                              }
                          }];
}

- (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.
     */

    // Clear badge and update installation, required for auto-incrementing badges.
    if (application.applicationIconBadgeNumber != 0) {
        application.applicationIconBadgeNumber = 0;
        [[PFInstallation currentInstallation] saveInBackground];
    }

    // Clears out all notifications from Notification Center.
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    application.applicationIconBadgeNumber = 1;
    application.applicationIconBadgeNumber = 0;


    NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];
    FBSession *appLinkSession = [[FBSession alloc] initWithPermissions:permissionsArray];
    [FBSession setActiveSession:appLinkSession];
    // ... and open it from the App Link's Token.
    [appLinkSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        // Forward any errors to the FBLoginView delegate.
        if (error) {
            NSLog(@"%@", error);
        }
    }];
}

1 Answer 1

0

OKay,here is my code: Do whatever you like in the success block.

+ (void)checkPublishPermission:(NSString*)permission success:(void(^)(void))successCallback fail:(void(^)(void))failCallback
{
    // Check for publish permissions
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  NSDictionary *permissions= [(NSArray *)[result data] objectAtIndex:0];
                                  if (![permissions objectForKey:permission]){
                                      // permission not found, ask for it
                                      [FBHelper requestPublishPermission:permission success:successCallback fail:failCallback];
                                  } else {
                                      // Publish permissions found, publish the OG story
                                      successCallback();
                                  }

                              } else {
                                  // There was an error, handle it
                                  // See https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"%@", [error debugDescription]);
                              }
                          }];
}

+ (void)requestPublishPermission:(NSString*)permission success:(void(^)(void))successCallback fail:(void(^)(void))failCallback
{
    // Request publish_actions
    [FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:permission]
                                          defaultAudience:FBSessionDefaultAudienceFriends
                                        completionHandler:^(FBSession *session, NSError *error) {
                                            if (!error) {
                                                if ([FBSession.activeSession.permissions
                                                     indexOfObject:permission] == NSNotFound){
                                                    // Permission not granted, tell the user we will not publish
                                                    failCallback();
                                                } else {
                                                    // Permission granted, publish the OG story
                                                    successCallback();
                                                }

                                            } else {
                                                // There was an error, handle it
                                                // See https://developers.facebook.com/docs/ios/errors/
                                                NSLog(@"%@", [error localizedDescription]);
                                            }
                                        }];
}
6
  • the two methods are in a helper class called FBHelper which is created by me, you can rename it. checkPublishPermission is a method to check the session's permissions include "permission" or not. If not, it would call requestPublishPermission which would ask for the new publish permissions. (read permission doesn't work here, simple changes). In checkPublishPermission, if the session has that permission it would call success block, otherwise call failCallback.
    – Horst
    Dec 26, 2013 at 5:31
  • In requestPublishPermission, it would call success block which is passed by checkPublishPermission, if user agreed to give permission. If user cancel it, call fail block. If there is any error in FB request, it would NSLog the error.
    – Horst
    Dec 26, 2013 at 5:32
  • I'm implementing it atm.. give me a sec.
    – SaifDeen
    Dec 26, 2013 at 5:42
  • I'm trying to pass to reuestPublishPermission an Array of Permission.. Any idea how to implement this?´
    – SaifDeen
    Dec 26, 2013 at 6:25
  • Sorry i tried everything.. Your code isnt helping: message = "An active access token must be used to query information about the current user.";
    – SaifDeen
    Dec 26, 2013 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.