2

我正在尝试将 Facebook 集成到我的应用程序中。我需要用户的生日、姓名和电子邮件。

我已经集成了Facebookloginview来自 sdk 的。

如果我点击登录,应用程序会切换到 Safari,并且页面显示我已经授权了该应用程序。但我无法收到电子邮件或生日

如果我更改了类似的权限user_location,然后注销并登录应用程序显示它已经被授权但不是!

如果我在 xib 上实现一个按钮,然后添加到 ibaction,则此代码有效。

AppDelegate *appDelegate    [[UIApplication sharedApplication] delegate];

// If the person is authenticated, log out when the button is clicked.
// If the person is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
    [appDelegate closeSession];
} else {
    // The person has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];
}


}

为什么它不适用于 facebookloginview?

这是我的 facebook loginviewcontroller.m

- (void)viewDidLoad
{
[super viewDidLoad];
loginview = [[FBLoginView alloc]init];
loginview.readPermissions = @[@"email"];
//Facebook start
[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(sessionStateChanged:)
 name:FBSessionStateChangedNotification
 object:nil];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:NO];
if (!appDelegate.session.isOpen) {
    // create a fresh session object
    appDelegate.session = [[FBSession alloc] init];
    // if we don't have a cached token, a call to open here would cause UX for login to
    // occur; we don't want that to happen unless the user clicks the login button, and so
    // we check here to make sure we have a token before calling open
    if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
        // even though we had a cached token, we need to login to make the session usable
        [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            // we recurse here, in order to update buttons and labels
            [self updateView];
        }];
    }
}
}
-(void) loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.loginlabel.text = @"";
self.FBimageViewOutlet.profileID = nil;
NSLog(@"bin hier geklickt3");    
}
-(void) loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
loginView.readPermissions = @[@"email"];
 FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
                                  NSDictionary<FBGraphUser> *my,
                                  NSError *error) {
    self.loginlabel.text = [NSString stringWithFormat:@"Hi, %@ %@ geboren am %@",my.first_name,my.last_name,my.birthday];
    self.FBimageViewOutlet.profileID = my.id;
    self.picView.profileID =my.id;}];

//self.loginlabel.text = [NSString stringWithFormat:@"Hi, %@ %@ geboren am %@",user.first_name,user.last_name,user.birthday];
self.FBimageViewOutlet.profileID = user.id;
//Hier wird gender von Facebook abgefragt
NSLog(@"%@",[user objectForKey:@"gender"]);
NSLog(@"%@",[user objectForKey:@"locale"]);
NSLog(@"%@",[user objectForKey:@"age_range"]);
NSLog(@"%@",[user objectForKey:@"username"]);
NSLog(@"%@",[user objectForKey:@"link"]);
NSLog(@"%@",[user objectForKey:@"location"]);
NSLog(@"%@",[user objectForKey:@"hometown"]);
NSLog(@"%@",[user objectForKey:@"birthday"]);
NSLog(@"%@",[user objectForKey:@"last_name"]);
NSLog(@"%@",[user objectForKey:@"first_name"]);
NSLog(@"%@",[user objectForKey:@"email"]);
NSLog(@"%@",[user objectForKey:@"user_email"]);
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject:[user objectForKey:@"gender"] forKey:@"user_gender"];
[defaults setObject:[user objectForKey:@"locale"] forKey:@"user_locale"];
[defaults setObject:[user objectForKey:@"age_range"] forKey:@"user_age_range"];
[defaults setObject:[user objectForKey:@"username"] forKey:@"user_username"];
[defaults setObject:[user objectForKey:@"link"] forKey:@"user_link"];
[defaults setObject:[user objectForKey:@"location"] forKey:@"user_location"];
[defaults setObject:[user objectForKey:@"hometown"] forKey:@"user_hometown"];
[defaults setObject:[user objectForKey:@"birthday"] forKey:@"user_birthday"];
[defaults setObject:[user objectForKey:@"last_name"] forKey:@"user_lastname"];
[defaults setObject:[user objectForKey:@"first_name"] forKey:@"user_firstname"];
}
- (void)loginView:(FBLoginView *)loginView
  handleError:(NSError *)error {
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
// If the person is authenticated, log out when the button is clicked.
// If the person is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
    [appDelegate closeSession];
} else {
    // The person has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];
}
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser) {
    // If the SDK has a message for the user, surface it. This conveniently
    // handles cases like password change or iOS6 app slider state.
    alertTitle = @"Facebook Error";
    alertMessage = error.fberrorUserMessage;
} else if (error.fberrorCategory == FBErrorCategoryAuthenticationReopenSession) {
    // It is important to handle session closures since they can happen
    // outside of the app. You can inspect the error for more context
    // but this sample generically notifies the user.
    alertTitle = @"Session Error";
    alertMessage = @"Your current session is no longer valid. Please log in again.";
} else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
    // The user has cancelled a login. You can inspect the error
    // for more context. For this sample, we will simply ignore it.
    NSLog(@"user cancelled login");
} else {
    // For simplicity, this sample treats other errors blindly.
    alertTitle  = @"Unknown Error";
    alertMessage = @"Error. Please try again later.";
    NSLog(@"Unexpected error:%@", error);
}
if (alertMessage) {
    [[[UIAlertView alloc] initWithTitle:alertTitle
                                message:alertMessage
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}
}
4

0 回答 0