简体   繁体   English

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

[英]Login with Facebook and fetch email and user info for iOS 7

I am newbie in iOS and I am working on app in which I need to integrate facebook with my App just for login purpose and to fetch user info on login user. 我是iOS的新手,我正在开发需要将Facebook与我的应用程序集成以用于登录目的并获取登录用户信息的应用程序。 I just read few tutorial but those are for sharing purpose, which I don't need those. 我只读了一些教程,但这些都是出于共享目的,而我不需要这些。 I also want that if user login from facebook that should be save in settings (my client requirement)? 我还希望如果来自Facebook的用户登录名应保存在设置中(我的客户要求)? Kindly suggest me few links. 请给我几个链接。 Thanks 谢谢

You can implement it with Facebook SDK. 您可以使用Facebook SDK实施它。

There are two ways to implement Facebook login in your iOS app: using the Facebook login button or implementing your custom login UI using API calls. 有两种方法可以在iOS应用中实现Facebook登录:使用Facebook登录按钮或使用API​​调用实现自定义登录UI。

This is how you request for permissions for information to want to read. 这是您请求要阅读的信息的权限的方式。

FBLoginView *loginView = 
    [[FBLoginView alloc] initWithReadPermissions:
        @[@"public_profile", @"email"]];

When you implement FBLoginViewDelegate 当您实现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;
}

https://developers.facebook.com/docs/facebook-login/ios/v2.0 https://developers.facebook.com/docs/facebook-login/ios/v2.0

Above link has all you need with sample/example code. 上面的链接提供了您需要的所有示例/示例代码。

If you don't want to use Facebook SDK. 如果您不想使用Facebook SDK。 Same thing can be achieved using Social Framework. 使用社交框架可以实现相同的目的。

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

In YourClass.h file. 在YourClass.h文件中。

#import <FacebookSDK/FacebookSDK.h>

@interface YourClass : UIViewController<FBLoginViewDelegate>

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

In YourClass.m file. 在YourClass.m文件中。

@synthesize loginFacebook;

In ViewDidLoad 在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];
 }

pragma mark Facebook 实用标记Facebook

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM