Ios Facebook登录-获取用户数据
我正在iOS上使用Parse作为后端构建一个应用程序 为此,我使用Parse Facebook SDK,首先将Facebook链接到我的Parse用户,然后查询他的数据并保存它们 问题是:有时我得不到一些信息,比如电子邮件(userData[@“eMail”]为零)、名字、名字……或者个人资料图片没有下载 并非总是如此(实际上非常罕见),而且不可能可靠地复制问题,以便我能够解决它。而且通常只缺少一个字段,而不是所有字段 因此,我的数据库有漏洞(当它是用户的电子邮件地址时就不酷了:我主要关心的问题) 下面是我编写方法的方式(我编辑了大部分if(error)管理,使其更短) 有什么想法吗 谢谢你们Ios Facebook登录-获取用户数据,ios,facebook,login,parse-platform,Ios,Facebook,Login,Parse Platform,我正在iOS上使用Parse作为后端构建一个应用程序 为此,我使用Parse Facebook SDK,首先将Facebook链接到我的Parse用户,然后查询他的数据并保存它们 问题是:有时我得不到一些信息,比如电子邮件(userData[@“eMail”]为零)、名字、名字……或者个人资料图片没有下载 并非总是如此(实际上非常罕见),而且不可能可靠地复制问题,以便我能够解决它。而且通常只缺少一个字段,而不是所有字段 因此,我的数据库有漏洞(当它是用户的电子邮件地址时就不酷了:我主要关心的问题
NSArray *permissionsArray = @[@"public_profile",@"email", @"user_friends"];
[PFFacebookUtils initializeFacebook];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (user.isNew) {
NSLog(@"User with facebook signed up and logged in!");
[self fbData:self];
} else {
NSLog(@"User with facebook logged in!");
[self fbDataAlreadyLog:self];
}
}];
然后调用我的“fbData”函数,将用户数据填入Parse:
-(IBAction)fbData:(id)sender{
[PFFacebookUtils initializeFacebook];
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *userCloud, NSError *error) {
if (!error) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = userCloud;
NSString *facebookID = userData[@"id"];
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];
PFUser *user = [PFUser currentUser];
if (userData[@"name"]) {
user[@"name"] = userData[@"name"];
}
if (userData[@"first_name"]) {
user[@"first_name"] = userData[@"first_name"];
}
if (userData[@"last_name"]) {
user[@"last_name"] = userData[@"last_name"];
}
if (userData[@"id"]) {
user[@"fbID"] = userData[@"id"];
}
if (userData[@"gender"]) {
user[@"gender"] = userData[@"gender"];
}
if (userData[@"email"]) {
user.email = userData[@"email"];
user.username = userData[@"email"];
}
if (userData[@"age_range"]) {
user[@"age_range"] = userData[@"age_range"];
}
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:pictureURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished,NSURL *imageURL)
{
if (image)
{
// do something with image
NSData *imageNSData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"profilePic" data:imageNSData];
user[@"image"] = imageFile;
[user saveInBackgroundWithBlock];
//Once the Save Block is done I managed a few more thing before moving on to the next ViewController
}
}];
}];
-(iAction)fbData:(id)发送方{
[PFFacebookUtils initializeFacebook];
FBRequest*request=[FBRequest requestForMe];
//向Facebook发送请求
[请求startWithCompletionHandler:^(FBRequestConnection*连接,NSDictionary*用户云,NSError*错误){
如果(!错误){
//结果是一个包含用户Facebook数据的字典
NSDictionary*userData=userCloud;
NSString*facebookID=userData[@“id”];
NSURL*pictureURL=[NSURL URLWithString:[NSString stringWithFormat:@]https://graph.facebook.com/%@/图片?类型=大并返回_ssl_resources=1”,facebookID]];
PFUser*user=[PFUser currentUser];
如果(userData[@“name”]){
用户[@“名称”]=用户数据[@“名称”];
}
if(userData[@“first_name”]){
用户[@“first_name”]=userData[@“first_name”];
}
如果(用户数据[@“姓氏”]){
用户[@“姓氏”]=用户数据[@“姓氏”];
}
if(userData[@“id”]){
用户[@“fbID”]=用户数据[@“id”];
}
如果(用户数据[@“性别”]){
用户[@“性别”]=用户数据[@“性别”];
}
如果(用户数据[@“电子邮件”]){
user.email=userData[@“email”];
user.username=userData[@“email”];
}
如果(用户数据[@“年龄范围”]){
用户[@“年龄范围”]=用户数据[@“年龄范围”];
}
SDWebImageManager*manager=[SDWebImageManager共享管理器];
[manager下载图像,URL:pictureURL
选项:0
进度:^(NSInteger接收大小,NSInteger预期大小)
{
//进程跟踪码
}
已完成:^(UIImage*image,NSError*error,SDImageCacheType cacheType,BOOL finished,NSURL*imageURL)
{
如果(图像)
{
//用图像做点什么
NSData*imageNSData=UIImagePNGRepresentation(图像);
PFFile*imageFile=[PFFile fileWithName:@“profilePic”数据:imageNSData];
用户[@“image”]=imageFile;
[用户saveInBackgroundWithBlock];
//保存块完成后,在转到下一个ViewController之前,我又做了一些事情
}
}];
}];
像这样试试
NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];