1

我是iOS的初学者。我正在尝试创建一个应用程序并在 stackoverflow 和其他一些站点中引用了很多帖子,我使用以下代码访问 Facebook 帐户。

AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

if (appDelegate.session.isOpen) {
    [appDelegate.session closeAndClearTokenInformation];

} else {
    if (appDelegate.session.state != FBSessionStateCreated) {
        appDelegate.session = [[FBSession alloc] init];
    }
    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
    }];
}

之后在一个函数中,我使用以下代码访问用户的 Facebook 详细信息。

    if (FBSession.activeSession.isOpen) {
    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             _FirstName.text = user.first_name;
             regisrationdetails.fbid = user.id;
             _LastName.text=user.last_name;
             _EmailAddress.text=user.email;
             NSArray *locationarray=[[NSArray alloc]initWithObjects:user.location,nil];
             _City.text=[locationarray objectAtIndex:1];
             NSLog(@"%@",user.first_name);

         }
     }];

没有显示错误,但我无法从 Facebook 的文本字段中获取信息。如果擅长此操作的人知道如何访问信息,请帮助我。

4

2 回答 2

1

尝试这个..

- (void)fetchFacebookUserInfo {

    if ( FBSession.activeSession.isOpen) {

    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                           id<FBGraphUser> user,
                                                           NSError *error) {
        if (!error) {
            NSString *name = user.name;
            NSString *userName = user.username;
            NSString *firstName = user.first_name;
            NSString *lastName = user.last_name;
            NSString *email1 = [user objectForKey:@"email"];
            NSString *birthday1 = user.birthday;
            NSString *locale = [user objectForKey:@"locale"];
            NSString *location = [user.location objectForKey:@"name"];

      }

  }];
 }
于 2013-10-30T09:20:09.437 回答
1
- (IBAction)getMeButtonTapped:(id)sender {
if(!_accountStore)
    _accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:@{ACFacebookAppIdKey: @"571438296262222", ACFacebookPermissionsKey: @[@"email"]}
                                    completion:^(BOOL granted, NSError *error) {
                                        if(granted){
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                            _facebookAccount = [accounts lastObject];
                                            NSLog(@"Success");

                                            [self me];
                                        }else{
                                            // ouch
                                            NSLog(@"Fail");
                                            NSLog(@"Error: %@", [error localizedDescription]);
                                        }
                                    }];
}

- (void)me
{
NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];

SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                          requestMethod:SLRequestMethodGET
                                                    URL:meurl
                                             parameters:nil];

merequest.account = _facebookAccount;

[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", meDataString);

}];}

使用这个希望这对你有帮助,还添加社交框架以及帐户框架

于 2013-10-30T09:04:24.363 回答