使用Facebook登录并获取iOS 7的电子邮件和用户信息

使用Facebook登录并获取iOS 7的电子邮件和用户信息,ios,objective-c,facebook,Ios,Objective C,Facebook,我是iOS的新手,我正在开发一个应用程序,在这个应用程序中,我需要将facebook与我的应用程序集成,以便登录并获取登录用户的用户信息。我只是读了一些教程,但这些都是为了分享,我不需要这些。我还想知道,如果用户从facebook登录,应该保存在设置中(我的客户要求)?请建议我几个链接。谢谢你可以用Facebook SDK实现它。 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook

我是iOS的新手,我正在开发一个应用程序,在这个应用程序中,我需要将facebook与我的应用程序集成,以便登录并获取登录用户的用户信息。我只是读了一些教程,但这些都是为了分享,我不需要这些。我还想知道,如果用户从facebook登录,应该保存在设置中(我的客户要求)?请建议我几个链接。谢谢你可以用Facebook SDK实现它。

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                     requestMethod:SLRequestMethodGET
                                               URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
                                        parameters:nil];
request.account = _account; // This is the _account from your code
[request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) {
        NSError *deserializationError;
        NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError];

        if (userData != nil && deserializationError == nil) {
            NSString *email = userData[@"email"];
            NSLog(@"%@", email);
        }
    }
}]; 
在iOS应用程序中实现Facebook登录有两种方法:使用Facebook登录按钮或使用API调用实现自定义登录UI

这是您请求要读取的信息的权限的方式

FBLoginView *loginView = 
    [[FBLoginView alloc] initWithReadPermissions:
        @[@"public_profile", @"email"]];
当您实现FBLoginViewDelegate时

// This method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
  self.profilePictureView.profileID = user.id;
  self.nameLabel.text = user.name;
}
在YourClass.h文件中

#import <FacebookSDK/FacebookSDK.h>

@interface YourClass : UIViewController<FBLoginViewDelegate>

@property(nonatomic,retain) IBOutlet FBLoginView *loginFacebook;
在ViewDidLoad中

-(Void)ViewDidLoad
{

    loginFacebook =
        [[FBLoginView alloc] initWithPublishPermissions:[NSArray arrayWithObjects:@"email",@"user_friends",nil] defaultAudience:FBSessionDefaultAudienceFriends];

        for (id obj in loginFacebook.subviews)
        {
            if ([obj isKindOfClass:[UIButton class]])
            {
                UIButton * loginButton =  obj;

                UIImage *loginImage = [UIImage imageNamed:@"facebook-off.png"];
                [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
                [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
                [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
                //[loginButton sizeToFit];
            }
            if ([obj isKindOfClass:[UILabel class]])
            {
                UILabel * loginLabel =  obj;
                loginLabel.text =@""; //@"Log in to facebook";
                loginLabel.textAlignment = NSTextAlignmentCenter;
                loginLabel.frame =CGRectMake(123,149, 280, 55);// CGRectMake(0, 0, 271, 37);
            }
        }

        loginFacebook.delegate = self;

        if (IsIphone5)
        {
            loginFacebook.frame =CGRectMake(29, 249, 263, 54);
        }
        else
        {
            loginFacebook.frame =CGRectMake(29, 240, 263, 54);
        }

        [self.view addSubview:loginFacebook];

      [Super ViewDidLoad];
 }
布拉格马克脸谱
-(void)loginView显示日志用户:(FBLoginView*)loginView
{
}
-(无效)loginView蚀刻教育信息:(FBLoginView*)loginView
用户:(id)用户
{    
Nslog(@“==%@”,用户);
}
-(无效)loginView显示LoggeDoutUser:(FBLoginView*)loginView
{
NSHTTPPCookieStorage*cookieStorage=[NSHTTPPCookieStorage SharedHTTPPCookieStorage];
FBSession*会话=[FBSession activeSession];
[会话关闭和ClearTokenInformation];
//[FBSession setActiveSession:nil];
适用于(NSHTTPCookie*每个在cookieStorage.cookies中)
{
[Cookies存储删除Cookie:每个];
}
}
-(无效)loginView:(FBLoginView*)loginView
handleError:(n错误*)错误
{
NSLog(@“=%@”,错误。本地化描述);
}

谢谢Balpreet,这个帐户是否也可以保存在设置facebook选项中?@Josh:facebook sdk可能会保存会话信息。但其他信息,如用户名、电子邮件等,您需要在应用程序中管理/存储。是的,您可以在集成的应用程序中或在facebook应用程序(如果我们使用SSO)中设置此持久会话,但我认为此信息不会保存在设置中。我更关心这一点,因为在此之前我必须将其与iOS 7集成。那么,你知道我该如何实现我的目标吗?1)当你说“设置”时,你指的是哪些确切的设置?2) 你到底想保留什么信息?3) 您是否担心无法支持iOS 6?1。像进入设置(应用程序)和botton,你会看到facebook和twitter。2.电子邮件,名字,姓氏。3.不,我确信此应用程序不适用于ios 6或更低版本的操作系统。这是所有我的客户要求,它应该保存在设置登录后获取信息。
-(Void)ViewDidLoad
{

    loginFacebook =
        [[FBLoginView alloc] initWithPublishPermissions:[NSArray arrayWithObjects:@"email",@"user_friends",nil] defaultAudience:FBSessionDefaultAudienceFriends];

        for (id obj in loginFacebook.subviews)
        {
            if ([obj isKindOfClass:[UIButton class]])
            {
                UIButton * loginButton =  obj;

                UIImage *loginImage = [UIImage imageNamed:@"facebook-off.png"];
                [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
                [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
                [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
                //[loginButton sizeToFit];
            }
            if ([obj isKindOfClass:[UILabel class]])
            {
                UILabel * loginLabel =  obj;
                loginLabel.text =@""; //@"Log in to facebook";
                loginLabel.textAlignment = NSTextAlignmentCenter;
                loginLabel.frame =CGRectMake(123,149, 280, 55);// CGRectMake(0, 0, 271, 37);
            }
        }

        loginFacebook.delegate = self;

        if (IsIphone5)
        {
            loginFacebook.frame =CGRectMake(29, 249, 263, 54);
        }
        else
        {
            loginFacebook.frame =CGRectMake(29, 240, 263, 54);
        }

        [self.view addSubview:loginFacebook];

      [Super ViewDidLoad];
 }
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{

}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user
{    
    Nslog(@"=== %@",User);
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    FBSession* session = [FBSession activeSession];
    [session closeAndClearTokenInformation];
    //        [FBSession setActiveSession:nil];
    for (NSHTTPCookie *each in cookieStorage.cookies)
    {
        [cookieStorage deleteCookie:each];
    }
}
- (void)loginView:(FBLoginView *)loginView
      handleError:(NSError *)error
{
    NSLog(@"== %@",error.localizedDescription);
}