简体   繁体   中英

FBSDKLoginManager not open login Dialog in native ios apps

I am new to iPhone development ,i want to do a facebook login from my native iOS simple application using latest facebook sdk 4. something ,i just want to open a facebook login dialog from Custom UIButton click,in my apps and for that i do below code in my button click method..

- (IBAction)didTapLoginFaceBook:(id)sender {

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error

        NSLog(@"Unexpected login error: %@", error);
        NSString *alertMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging in. Please try again later.";
        NSString *alertTitle = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops";
        [[[UIAlertView alloc] initWithTitle:alertTitle
                                        message:alertMessage
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];


        } else if (result.isCancelled) {
            // Handle cancellations
        } else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if ([result.grantedPermissions containsObject:@"email"]) {
                // Do work
            }
        }
    }];


}

This code is work something different,but i want to open a login dialog form Custom UIButton Click in the native iOS apps.

So,please suggest the right way with some code,thanks in advance

Facebook login 4.x with custom button in Objective-C

- (IBAction)btnFacebookPressed:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    login.loginBehavior = FBSDKLoginBehaviorBrowser;
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
                 [login logOut];
             }
         }
     }];
}

- (void)fetchUserInfo {
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);

             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
    }
}

Facebook login 4.x with custom button in Swift

@IBAction func btnFacebookPressed(_ sender: Any) {

    let loginManager = FBSDKLoginManager()
    loginManager.loginBehavior = FBSDKLoginBehavior.browser;

    loginManager.logIn(withReadPermissions: ["email"], from: self, handler: {(result : FBSDKLoginManagerLoginResult?, error : Error?) -> Void in
        if error != nil {
            // Process error
        }
        else if (result?.isCancelled)! {
            // Handle cancellations
        }
        else if (result?.grantedPermissions.contains("email"))! {
            print("result is: \(result)");
            self.fetchUserInfo()
            loginManager.logOut()
        }
    })
}


func fetchUserInfo() {
    if let accessToken = FBSDKAccessToken.current()?.tokenString {
        print("Token is available : \(accessToken)")

        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"]).start(completionHandler: { (connection : FBSDKGraphRequestConnection?, result : Any?, error : Error?) in

            if let error = error {
                print("Error \(error)")
            }
            else if let result  = result {
                print("result:\(result)")
            }
        })
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM