0

这是我第一次尝试将 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];

    }];
}
4

2 回答 2

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);

    }];}];
}
于 2013-12-02T07:20:33.297 回答
1

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

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

这是Facebook 权限密钥和描述

于 2013-11-01T07:48:08.550 回答