Ios 未使用Facebook SDK在Facebook中对用户进行签名
我正在尝试从服务器检索数据。当json请求成功时,服务器应该返回用户的基本信息 我的问题是,当我将此代码删除到.plist文件时:Ios 未使用Facebook SDK在Facebook中对用户进行签名,ios,json,facebook,Ios,Json,Facebook,我正在尝试从服务器检索数据。当json请求成功时,服务器应该返回用户的基本信息 我的问题是,当我将此代码删除到.plist文件时: <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb***</string> </a
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb***</string>
</array>
</dict>
</array>
这将显示要弹出的登录视图:
以及我需要正确获取的所有数据。但我想要的是在浏览器中登录,然后返回应用程序。。因此,我在.plist文件中包含了上面的代码
以下是我获取值的代码:
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[NSUserDefaults standardUserDefaults] valueForKey:FB_DISPLAY_NAME_TEXT] !=nil) {}else{
NSLog(@"@@wew: %@", [[NSUserDefaults standardUserDefaults] valueForKey:FB_DISPLAY_NAME_TEXT]);
[webService getDataFromURL:[NSString stringWithFormat:@"%@%@", ROOT_SERVER_URL, FB_URL_LOGIN]];
}
这段代码在登录弹出时获取我需要的数据。我需要用户先登录,所以我把请求放在那里。我的代码中有问题吗?首先将openurl代码放在appdelegate.m文件中 然后使用以下代码登录facebook:
NSArray *permissions = [NSArray arrayWithObjects:@"email", @"public_profile", @"user_friends",nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:TRUE
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (!error) {
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading...";
hud.dimBackground = NO;
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"picture.type(large),id,birthday,email,name,gender,location" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
//username
if (!error) {
NSString *facebookId = user.objectID;
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *email = ([user objectForKey:@"email"] == nil) ? @"" : [user objectForKey:@"email"];
// NSString *username = (user.username == nil) ? @"" : user.username;
NSString *gender = [user objectForKey:@"gender"];
NSString *birthday = user.birthday;
NSString *userLocation = ([[user objectForKey:@"location"]objectForKey:@"name"] == nil) ? @"" : [user objectForKey:@"location"];
NSString *year = [birthday substringFromIndex:6];
NSString *monthstr = [birthday substringFromIndex:3];
NSString *month = [monthstr substringToIndex:2];
NSString *date = [birthday substringToIndex:2];
birthday = [NSString stringWithFormat:@"%@-%@-%@",date,month,year];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[result objectForKey:@"picture"] objectForKey:@"data"] valueForKey:@"url"]]]];
NSData *dataImg = UIImageJPEGRepresentation(image, 0.5);
userDict = [[NSMutableDictionary alloc]init];
[userDict setObject:firstName forKey:@"firstName"];
[userDict setObject:lastName forKey:@"lastName"];
[userDict setObject:[email RemoveNull] forKey:@"email"];
[userDict setObject:email forKey:@"username"];
[userDict setObject:dataImg forKey:@"profilephoto"];
[userDict setObject:gender forKey:@"gender"];
[userDict setObject:facebookId forKey:@"facebookId"];
[userDict setObject:userLocation forKey:@"location"];
[self performSelector:@selector(signUpUser:) withObject:userDict afterDelay:0.1];
}
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}];
}
}];
}
}];
谢谢你。很抱歉反应太晚,我很难修复我的应用程序。
- (void) postRequestFromUrl: (NSString *) urlString withDictionary: (NSDictionary *) post{
NSURL *url = [NSURL URLWithString:urlString];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
NSString * postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
//url where u will send data
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: postData];
[request setValue:[NSString stringWithFormat:@"%d", [postLength length]] forHTTPHeaderField:@"Content-Length"];
NSLog(@"@@REQUEST: %@", request);
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}
NSArray *permissions = [NSArray arrayWithObjects:@"email", @"public_profile", @"user_friends",nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:TRUE
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (!error) {
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading...";
hud.dimBackground = NO;
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"picture.type(large),id,birthday,email,name,gender,location" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
//username
if (!error) {
NSString *facebookId = user.objectID;
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *email = ([user objectForKey:@"email"] == nil) ? @"" : [user objectForKey:@"email"];
// NSString *username = (user.username == nil) ? @"" : user.username;
NSString *gender = [user objectForKey:@"gender"];
NSString *birthday = user.birthday;
NSString *userLocation = ([[user objectForKey:@"location"]objectForKey:@"name"] == nil) ? @"" : [user objectForKey:@"location"];
NSString *year = [birthday substringFromIndex:6];
NSString *monthstr = [birthday substringFromIndex:3];
NSString *month = [monthstr substringToIndex:2];
NSString *date = [birthday substringToIndex:2];
birthday = [NSString stringWithFormat:@"%@-%@-%@",date,month,year];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[result objectForKey:@"picture"] objectForKey:@"data"] valueForKey:@"url"]]]];
NSData *dataImg = UIImageJPEGRepresentation(image, 0.5);
userDict = [[NSMutableDictionary alloc]init];
[userDict setObject:firstName forKey:@"firstName"];
[userDict setObject:lastName forKey:@"lastName"];
[userDict setObject:[email RemoveNull] forKey:@"email"];
[userDict setObject:email forKey:@"username"];
[userDict setObject:dataImg forKey:@"profilephoto"];
[userDict setObject:gender forKey:@"gender"];
[userDict setObject:facebookId forKey:@"facebookId"];
[userDict setObject:userLocation forKey:@"location"];
[self performSelector:@selector(signUpUser:) withObject:userDict afterDelay:0.1];
}
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}];
}
}];
}
}];