[英]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];
}
- (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.