在ios app中显示facebook用户详细信息

时间:2013-12-17 12:31:03

标签: ios iphone objective-c facebook

用户在Facebook上播放我的应用程序后,我不会显示他的名字和个人资料照片,我从AppDelegate类获取数据并将其添加到视图控制器中的属性(UserNameLable),

在AppDelegate中

所有nslogs都显示数据
在视图控制器中,nslogs显示为null 如何在mainViewController中将数据响亮到我的属性?

在AppDelegate中

这是代码

AppDelegate.h

@property (retain, nonatomic) NSString *first_name_property;

AppDelegate.m

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:

            [[NSNotificationCenter defaultCenter]
             postNotificationName:SCSessionStateChangedNotification
             object:session];


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

                    if (error) {

                        NSLog(@"error");
                    }

                    else
                    {
                        NSLog(@"FB user first name:%@",user.first_name);
                        NSLog(@"FB user last name:%@",user.last_name);

                        // Add the data to the property I crated 
                        first_name_property = user.first_name;


                    }
                }];



            }

            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;

        default:
            break;

//这是在我的视图中显示数据的代码

mainViewController.h

@property (strong, nonatomic) IBOutlet UILabel *UserNameLable;

mainViewController.m

-(void)viewDidAppear:(BOOL)animated{

    if (!UserNameLable) {
        UserNameLable.text=((AppDelegate *)[UIApplication sharedApplication].delegate).first_name_property;

    }
    else{
        UserNameLable.text=@"not set";}
}

1 个答案:

答案 0 :(得分:2)

使用 first_name_property 代替first_name。

尝试

UserNameLable.text=((AppDelegate *)[UIApplication sharedApplication].delegate).first_name_property;