简体   繁体   中英

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. 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)? Kindly suggest me few links. Thanks

You can implement it with 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.

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

// 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

Above link has all you need with sample/example code.

If you don't want to use 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.

#import <FacebookSDK/FacebookSDK.h>

@interface YourClass : UIViewController<FBLoginViewDelegate>

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

In YourClass.m file.

@synthesize loginFacebook;

In 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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