Facebook 导入详情 ios

Posted

技术标签:

【中文标题】Facebook 导入详情 ios【英文标题】:Facebook importing details ios 【发布时间】:2013-11-01 07:34:39 【问题描述】:

这是我第一次尝试将 Facebook 集成到我的 ios 应用程序中。 我正在尝试从 Facebook 导入名字、姓氏和其他一些详细信息。我可以登录 Facebook,但无法导入详细信息。我认为我的代码在某个地方是错误的,我不知道在哪里!

    if (appDelegate.session.isOpen) 
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                           id<FBGraphUser> user,
                                                           NSError *error2) 

        regdet.firstname = user.first_name;
        regdet.lastname = user.last_name;
        regdet.email = [user objectForKey:@"email"];
        regdet.address1 = [user objectForKey:@"locale"];
        regdet.city = [user.location objectForKey:@"name"];
        NSLog(@"firstname %@",regdet.firstname);


    ];


 else 
    if (appDelegate.session.state != FBSessionStateCreated) 
        // Create a new, logged out session.
        appDelegate.session = [[FBSession alloc] init];
    

    // if the session isn't open, let's open it now and present the login UX to the user
    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error1) 


        [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                               id<FBGraphUser> user,
                                                               NSError *error2) 

                regdet.firstname = user.first_name;
                regdet.lastname = user.last_name;
                regdet.email = [user objectForKey:@"email"];
                regdet.address1 = [user objectForKey:@"locale"];
                regdet.city = [user.location objectForKey:@"name"];
                NSLog(@"firstname %@",regdet.firstname);


        ];
         [self performSegueWithIdentifier: @"Facebooksegue" sender: self];

    ];

【问题讨论】:

【参考方案1】:

试试这个

FBLoginView *loginView=[[FBLoginView alloc]init];
loginView.delegate=self;
loginView.readPermissions = @[@"first_name",
                              @"last_name",
                              @"location",
                              @"id",
                              @"access_token",
                              @"email"];
NSArray* permissions =  [NSArray arrayWithObjects: @"email", nil];
loginView.readPermissions = @[@"email"];
loginView.readPermissions=permissions;



    [FBSession openActiveSessionWithReadPermissions:Nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) 





[FBRequestConnection
 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                   id<FBGraphUser> user1,
                                   NSError *error)
    
        if (!error)
        

            firstname=user1.first_name;
            lastname=user1.last_name;
            city=[user1.location objectForKey:@"name"];
            email=user1[@"email"];
            fbid=user1.id;

            [[NSUserDefaults standardUserDefaults]setObject:Loggedin forKey:@"token"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        
        NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@action=currfbuser&email=%@&fb_id=%@",MainURL,email,fbid ]];
        NSError *errors;
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
        NSString  *status = json[@"status"];
        user = json[@"user"];
        if ([status isEqualToString:@"success"])
        
            [self performSegueWithIdentifier: @"LogIN" sender: self];
        
        else if (!([fbid isEqualToString:@""]))
        
            [self performSegueWithIdentifier: @"Facebooksegue" sender: self];
        
        NSLog(@"%@",firstname);
        NSLog(@"%@",lastname);
        NSLog(@"%@",city);
        NSLog(@"%@",email);
        NSLog(@"%@",fbid);

    ];];

【讨论】:

【参考方案2】:

Here is Facebook 文档和示例。您需要请求额外的许可。

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"]
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error)   
                                  // Respond to session state changes,   
                                  // ex: updating the view
                              ];

Here isFacebook 权限密钥和描述

【讨论】:

上述从 Facebook 服务器获取数据的方式是否正确\? 抓取是正确的,但首先您需要获得许可。您需要具有正确权限的公开会话。创建会话时,您应该添加权限。 FBSession *session = [[FBSession activeSession] initWithPermissions:@[@"basic_info", @"email"]];

以上是关于Facebook 导入详情 ios的主要内容,如果未能解决你的问题,请参考以下文章

iOS Facebook 登录无需打开浏览器或原生 Facebook 应用程序

Fb 请求对话框错误地返回 nil - IOS Facebook

iOS Facebook 获取好友列表

iOS Facebook 分享问题

iOS Facebook 登录 - 链接器错误

iOS facebook 登录问题