用Facebooklogin并获取iOS 7的电子邮件和用户信息

我在iOS是新手,我正在开发应用程序,我需要将facebook与我的应用程序集成,仅用于login目的,并获取login用户的用户信息。 我只读了几个教程,但这些是为了分享的目的,我不需要这些。 我也希望如果用户从Facebooklogin,应该保存在设置(我的客户要求)? 请build议我几个链接。 谢谢

你可以用Facebook SDK来实现它。

有两种方法可以在您的iOS应用程序中实现Facebooklogin:使用Facebookloginbutton或使用API​​调用实现您的自定义loginUI。

这就是你如何请求信息的权限来阅读。

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