2014-09-01 205 views
1

我已经成功使用Facebook SDK登录并获取个人资料图片和名称。但它的观点是一致的。我想知道如何在登录后更改为其他视图。使用自定义按钮登录Facebook

AppDelegate.m文件

- (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文件

@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; 
} 
+0

u需要推视图控制器从获取数据 – 2014-09-01 09:19:16

+0

如果是使用自定义按钮操作方法 – 2014-09-01 09:19:40

+0

@ Anbu.Karthik,对不起,我不明白。登录按钮实际上是一个名为FBLoginView的视图。我如何自定义按钮操作? – Jenny 2014-09-01 09:57:34

回答

2

这是自定义按钮的方法:

- (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 
               { 
                [email protected]""; 
                [email protected]""; 
               } 

               */ 

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



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

               else 
               { 
                [email protected]""; 
               } 





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

所以你的意思是自定义按钮的能力比FBLoginView更多? – Jenny 2014-09-02 05:47:52

+0

FBloginview重定向到相同的视图后,我们继续任何定制,在这个自定义按钮的动作方法,我们可以定制我们自己 – 2014-09-02 05:50:54

+0

好吧,非常感谢:)! – Jenny 2014-09-02 07:28:51

0

在最新的FB SDK这不工作了。 FBSession(和FBRequest一起)已经被删除。

现在你必须使用FBSDKLoginManager类及其方法登录

- (void)logInWithReadPermissions:(NSArray *)permissions handler:(FBSDKLoginManagerRequestTokenHandler)handler; 

示例代码和获取用户信息:

FBSDKLoginManager* lm = [[FBSDKLoginManager alloc] init]; 
[lm logInWithReadPermissions:@[@"public_profile", @"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 

    // no error, so get user info 
    if (!error && !result.isCancelled) 
    { 
     [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] 
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

     if (!error) { 
      NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]); 
     } 
    }]; 
    } 

}]; 

如前所述here