0

I am trying to implement Facebook login into my app. I already do everything that Facebook developer page recommends me but I can't do this work correctly.

I have already put my bundle identifier in Facebook Dev App

Facebook Config:

Facebook Config

Xcode Project Config:

Xcode Project Config

In my AppDelegate I put the code that Facebook indicates

#import <FBSDKCoreKit/FBSDKCoreKit.h>

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {

            [[FBSDKApplicationDelegate sharedInstance] application:application
                                   didFinishLaunchingWithOptions:launchOptions];


            return YES;
        }

    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {

      BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                    openURL:url
                                                          sourceApplication: sourceApplication
                                                                 annotation: annotation
                      ];

        return handled;
    }

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {   
        [FBSDKAppEvents activateApp];   
    }

Also put the login button as shown in Facebook documentation

-(void) viewDidLoad() {
    self.loginButton = [[FBSDKLoginButton alloc] init];
    self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
    [self.loginButton addTarget:self action:@selector(buttonFacebookLoginClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.manager =[[FBSDKLoginManager alloc] init];
}

But in the method buttonFacebookLoginClicked when I handle the response I always have result.isCancelled as true.

In the app when I click on FB login button I am redirected to a new tab in safari that does not show anything and then when I click on OK button the result is cancelled.

My Facebook login button:

My Facebook login button

Safari view:

Safari view

There is my login function, and I always drop in the case that result.isCancelled is true, after click the OK button in Safari

- (IBAction)buttonFacebookLoginClicked:(id)sender {

  if ([FBSDKAccessToken currentAccessToken] != nil) {
    [self.manager logOut];
    //return
  }

  [self.manager logInWithReadPermissions: self.loginButton.readPermissions fromViewController: self handler: ^(FBSDKLoginManagerLoginResult* result, NSError* error ) {
    if (error != nil) {
      //According to Facebook:
      //Errors will rarely occur in the typical login flow because the login dialog
      //presented by Facebook via single sign on will guide the users to resolve any errors.

      // Process error
      [self.manager logOut];
    } else if (result.isCancelled) {
      // Handle cancellations
      for (NSString * p in self.loginButton.readPermissions) {
        NSLog(@"PERMISSION %@", p);
      }
      [self.manager logOut];
    } else {
      // If you ask for multiple permissions at once, you
      // should check if specific permissions missing
      BOOL allPermsGranted = true;

      //result.grantedPermissions returns an array of _NSCFString pointers
      NSArray *grantedPermissions = [[result grantedPermissions] allObjects];

//      let grantedPermissions = result.grantedPermissions.allObjects.map( {"\($0)"} )

      for (NSString *permission in self.loginButton.readPermissions) {
        for (NSString* grantedPerm in grantedPermissions) {
          NSRange range = [permission rangeOfString:grantedPerm];
          if (range.length == 0) {
            allPermsGranted = false;
            break;
          }
        }
      }
      if (allPermsGranted) {
        // Do work
        NSString * fbToken = [[result token] tokenString];
        NSString * fbUserID = [[result token] userID];

        FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                      initWithGraphPath:@"/me"
                                      parameters:@{@"fields": @"id,name,email"}
                                      HTTPMethod:@"GET"];

        [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
          NSLog(@"result %@", result);
          NSDictionary *dict = @{
                                 @"email" : @"email" ,
                                 @"providerID" : @"id",
                                 @"username" : @"name",
                                 @"provider" : @"facebook"
                                 };
          NSLog(@"DICT %@", dict);
          [self.delegate loginWithCredentials:dict];
          [self logUserLoggedInWithFacebookEvent];
        }];

      } else {
        //The user did not grant all permissions requested
        //Discover which permissions are granted
        //and if you can live without the declined ones
        NSLog(@"FACEBOOK LOGIN ERROR");
      }
    }


  }];

}

I think that is a less error but I can't find it.

halfer
  • 19,824
  • 17
  • 99
  • 186
origds
  • 1,047
  • 2
  • 11
  • 25
  • 1
    May I ask, why don't use Account framework as a better option? One more question if you want to go with FB SDK, Did you configure your . PLIST for facebook sdk? – Gagan_iOS Sep 19 '17 at 10:34
  • You are missing this part https://developers.facebook.com/docs/ios/getting-started/#xcode\ – Gagan_iOS Sep 19 '17 at 10:38
  • I have already do that in my .plist file @Gagan_iOS – origds Sep 19 '17 at 10:42
  • Can you show the code in `buttonFacebookLoginClicked:` method and delegate methods of `FBSDKLoginManager` if you have implemented those? – Adeel Miraj Sep 19 '17 at 10:44
  • Look at these links, they have the same issue 1. https://stackoverflow.com/questions/29321402/facebook-login-always-comes-back-as-cancelled-ios-swift 2. https://stackoverflow.com/questions/20876208/ios-facebook-sdk-user-cancelled-login-when-running-app-on-device 3. https://github.com/facebook/facebook-sdk-swift/issues/66 4. https://stackoverflow.com/questions/43064013/facebook-sharedialog-always-returns-canceled-on-completion 5. https://stackoverflow.com/questions/29358661/fbsdkloginmanager-loginwithpublishpermissions-always-returns-iscancelled-yes – Gagan_iOS Sep 19 '17 at 10:45
  • I have never used the Account framework and the app was configured for facebook sdk use I would liked if I could solve this problem instead of reimplement everything – origds Sep 19 '17 at 10:46
  • I have already tried this others solutions but no solution works for me @Gagan_iOS – origds Sep 19 '17 at 10:48
  • then share your FB integration code if possible on a repository either Dropbox or anyone as you wish. – Gagan_iOS Sep 19 '17 at 10:49
  • I just edited the question and put the code @Gagan_iOS – origds Sep 19 '17 at 10:52
  • ok ..let me check it..wait – Gagan_iOS Sep 19 '17 at 10:55
  • https://gist.github.com/origds/f93d6816d48c091be294d13f30f9ba2b this is the plist config. @Gagan_iOS – origds Sep 19 '17 at 10:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154782/discussion-between-origds-and-gagan-ios). – origds Sep 19 '17 at 10:57
  • 1
    here is working code that I have uploaded for you https://drive.google.com/file/d/0B7lSqbQq9-WuYU9qNlhOdWczU2M/view?usp=sharing Go & download. It is already inegrated with your FB app id – Gagan_iOS Sep 19 '17 at 12:17

2 Answers2

0

can you please check the 'capabilities' section from target settings? Please Enable the option of "Keychain sharing"..As FBSDK uses keychain to store access token.

Swap
  • 1
0

Seems to me that we might be missing the URL Types information, or other information at your Info.plist file. Normally you need to specify which kind of schemes your app can handle as a callback, so once you log in, safari calls back to your app with the correct info.

EDIT: the obfuscated data is a string like fb<number> in my case. Normally appears somewhere as "redirect url" in other OAUTH providers.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
jsdario
  • 6,477
  • 7
  • 41
  • 75
  • Alright! Did not see them in your question. Other problems might be related to having different build environments (debug, release, stage...) Info.plists might be different for each one. Good luck. Post your answer if you fin it! – jsdario Sep 20 '17 at 14:28