使用自定义按钮使用Facebook登录 [英] Login with Facebook using custom button

查看:221
本文介绍了使用自定义按钮使用Facebook登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用facebook SDK登录并获取个人资料图片和名称。但同样的看法。我想知道如何在登录后更改另一个视图。

I've successfully use facebook SDK to login and get profile picture and name. But It's in the same view. I'm wondering how to change to another view after login.

AppDelegate.m文件

AppDelegate.m file

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[FBSession activeSession] handleDidBecomeActive];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [[FBSession activeSession]handleOpenURL:url];
}

viewController.m文件

viewController.m file

@interface loginViewController () <FBLoginViewDelegate>
@property (weak, nonatomic) IBOutlet FBLoginView *loginView;
@property (weak, nonatomic) IBOutlet FBProfilePictureView *ProfilePic;
@property (weak, nonatomic) IBOutlet UILabel *ProfileName;

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

#pragma mark FBLoginviewDelegate
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
self.ProfilePic.profileID = user.objectID;
self.ProfileName.text = user.name;
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.ProfilePic.profileID = nil;
self.ProfileName.text = nil;
}


推荐答案

这是自定义按钮方法:

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:
                                  }
                              }];
    }

这篇关于使用自定义按钮使用Facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆