本文介绍了使用Facebook登录并获取iOS 7的电子邮件和用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS上是新手,我正在使用应用程序,我需要将Facebook与我的应用程序集成,仅用于登录目的,并获取登录用户的用户信息。我只读了几本教程,但这些是用于共享的目的,我不需要这些教程。我也想要,如果用户从Facebook登录应该保存在设置(我的客户要求)?请提供几点链接。感谢

解决方案

您可以使用Facebook SDK实现。



在iOS应用程序中实现Facebook登录有两种方法:使用Facebook登录按钮或使用API​​调用实现自定义登录界面。



是您如何请求获取信息的权限以阅读。

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

实现FBLoginViewDelegate

  //当用户信息被提取时,将调用此方法
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id< FBGraphUser>)user {
self.profilePictureView.profileID = user.id;
self.nameLabel.text = user.name;
}



以上链接包含所有您需要的示例/示例代码。



如果您不想使用Facebook SDK。

  SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@https://graph.facebook.com/me]
参数:nil];
request.account = _account; //这是你的代码中的_account
[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(@%@,电子邮件);
}
}
}];


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

这篇关于使用Facebook登录并获取iOS 7的电子邮件和用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:23