我正在尝试将Facebook整合到我的应用中。我需要用户的生日,姓名和电子邮件。
我已经整合了来自sdk的Facebookloginview
。
如果我点击登录,该应用程序将切换到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];
}
}