简体   繁体   English

获取 facebook 用户信息到另一个视图控制器

[英]fetch facebook user info to another view controller

I have a default's facebook sdk loginViewController, but at all the examples on facebookSDK page, they fetched user info in a same viewController, so what can I do if I want to store all user info (fetched by loginViewController) to use in another viewControllers?我有一个默认的 facebook sdk loginViewController,但是在 facebookSDK 页面上的所有示例中,它们都在同一个 viewController 中获取用户信息,所以如果我想存储所有用户信息(由 loginViewController 获取)以在另一个 viewController 中使用,我该怎么办?

Fetching user code from loginViewController从 loginViewController 获取用户代码

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {

    self.profilePictureView.profileID = user.id;
    self.nameLabel.text = [NSString stringWithFormat:@"Welcome,\n %@", user.name];
    NSLog(@"%@ has been got from loginViewFetchedUserInfo function",user.name);
    _loggedInUser = user;
}

// Implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
    self.statusLabel.text = @"Click button below to continue";
    [self.logoImage setHidden:YES];
    [self.profilePictureView setHidden:NO];
    [self.continueButton setHidden:NO];
    //[self pushToMainTabsBarController];
}

// Implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
    self.profilePictureView.profileID = nil;
    self.nameLabel.text = @"";
    self.statusLabel.text= @"Please login to continue!";
    [self.logoImage setHidden:NO];
    [self.profilePictureView setHidden:YES];
    [self.continueButton setHidden:YES];

}

In the other view controller you just need to insert a property like this在另一个视图控制器中,您只需要插入这样的属性

@property (nonatomic, strong) id<FBGraphUser> user;

and then for example, when you fetch the user data in the first method, set the property to the new view controller (already instanced obviously):然后例如,当您在第一种方法中获取用户数据时,将属性设置为新的视图控制器(显然已经实例化了):

_newViewController.user = user;

Or if you want instance the new view controller in another time, you can set the same property in the login view controller to store the user data temporally (as you already are doing with _loggedInUser = user; ) and then pass it to the new view controller at the right moment.或者,如果您想在另一个时间实例化新的视图控制器,您可以在登录视图控制器中设置相同的属性来临时存储用户数据(正如您已经使用_loggedInUser = user;所做的_loggedInUser = user; ),然后将其传递给新视图控制器在适当的时候。

You can store it in Core Data and then retrieving back in other view or pass it throug the segue method like您可以将其存储在 Core Data 中,然后在其他视图中检索或通过segue方法传递它,例如

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ToOtherViewController"]) {
        OtherViewController *OVC = (OtherViewController *)segue.destinationViewController;
        OVC.user = user;
    }
}

Using Core Data requires a little reading .使用 Core Data 需要一点阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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