簡體   English   中英

使用Facebook登錄並獲取iOS 7的電子郵件和用戶信息

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

我是iOS的新手,我正在開發需要將Facebook與我的應用程序集成以用於登錄目的並獲取登錄用戶信息的應用程序。 我只讀了一些教程,但這些都是出於共享目的,而我不需要這些。 我還希望如果來自Facebook的用戶登錄名應保存在設置中(我的客戶要求)? 請給我幾個鏈接。 謝謝

您可以使用Facebook SDK實施它。

有兩種方法可以在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;
}

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

上面的鏈接提供了您需要的所有示例/示例代碼。

如果您不想使用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);
        }
    }
}]; 

在YourClass.h文件中。

#import <FacebookSDK/FacebookSDK.h>

@interface YourClass : UIViewController<FBLoginViewDelegate>

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

在YourClass.m文件中。

@synthesize 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];
 }

實用標記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