且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用自定义按钮使用Facebook登录

更新时间:2022-02-10 07:03:36

这是自定义按钮方法:

this is custom button method :

- (void)viewDidLoad {
[super viewDidLoad];
 //  self.loginView.delegate = self;    //hide one 
 }


 - (IBAction)butFacebooklogin:(id)sender
{
NSArray *permissions = @[@"email",@"user_about_me",@"user_status",@"user_location"];

// OPEN Session!
[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                                  // if login fails for any reason, we alert
                                  if (error) {

                                      //NSLog(@"error %@",error);
                                      // show error to user.

                                  } else if (FB_ISSESSIONOPENWITHSTATE(status)) {

                                      // no error, so we proceed with requesting user details of current facebook session.
                                      //NSLog(@"we are here");
                                      //NSLog(@"----%@",[session accessTokenData].accessToken);

                                    //  NSString *tok = [session accessTokenData].accessToken;
                                     // NSLog(@"tok 2");



                                      [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                                          if (error) {

                                              NSLog(@"error:%@",error);


                                          }
                                          else
                                          {

                                             /*

                                              NSString *userlocation=[NSString stringWithFormat:@"%@",user.location[@"name"]];

                                              NSArray *items = [userlocation componentsSeparatedByString:@","];

                                              NSString *usercity, *userstate;

                                              if ([items count]>0) {
                                                  usercity=[items objectAtIndex:0];

                                                  userstate=[items objectAtIndex:1];

                                                  userstate = [userstate stringByReplacingOccurrencesOfString:@" " withString:@""];
                                              }
                                              else
                                              {
                                                  usercity=@"";
                                                  userstate=@"";
                                              }

                                              */

                                              NSString *email=[user objectForKey:@"email"];



                                              if (email.length>0) {
                                                  email=[user objectForKey:@"email"];
                                              }

                                              else
                                              {
                                                  email=@"";
                                              }





                                              NSString *userUpdate =[NSString stringWithFormat:@"user_id=%@&fname=%@&lname=%@",email,user.first_name,user.last_name,nil];

                                               NSLog(@" RequestString for get login details: %@",userUpdate); 

  // here add ur segue or push view method
                                          }
                                      }];
                                      // [self promptUserWithAccountName];   // a custom method - see below:
                                  }
                              }];
    }