当我在设置中添加帐户时尝试使用 Facebook 框架登录 Facebook 时遇到了同样的问题,但我使用以下代码解决了这个问题:
据我所知“com.facebook.sdk error2”。当在 iphone 中添加了一个帐户时。
应用代理.h
@property(强,非原子) FBSession *mysession;
只需在 appdelegate.m 中添加方法
-(void)openSessionWithAllowLoginUI:(BOOL)allowLoginUI{
if (!self.mysession.isOpen) {
// create a fresh session object
self.mysession = [[FBSession alloc] initWithPermissions:permissions];
}
[self.mysession openWithCompletionHandler:^(FBSession *session,
FBSessionState stat,
NSError *error){
[self sessionStateChanged:session
state:stat
error:error];
NSLog(@"Session Staet is = %u",stat);
switch (stat){
case FBSessionStateClosed:
break;
case FBSessionStateOpen:{
NSString *strAccessToken1 = [mysession accessToken];
NSLog(@"AccessToken = %@",strAccessToken1);
NSString *urlstring1 = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",strAccessToken1];
NSURL *url1 = [NSURL URLWithString:[urlstring1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *jsonRes = [NSString stringWithContentsOfURL:url1 encoding:NSUTF8StringEncoding error:nil];
NSDictionary *facebookData = [jsonRes JSONValue];
NSLog(@"FBSessionStateOpen = %@",facebookData);
NSString *strFBID = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@",[facebookData objectForKey:@"id"]]];
NSString *strName = [[NSString alloc]initWithString:[facebookData objectForKey:@"name"]];
NSLog(@"FBSessionStateOpen = %@",strName);
NSString *ProfileImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture",strFBID];
NSLog(@"Profile image URL is = %@",ProfileImageURL);
NSString *strImageURl = [[NSString alloc]initWithString:ProfileImageURL];
strAccessToken = [[NSString alloc]initWithString:strAccessToken1];
strAppUserName = [[NSString alloc]initWithString:strName];
strFacebookUsername = [[NSString alloc]initWithString:strName];
strAppUserProfileImage = [[NSString alloc]initWithString:strImageURl];
strFacebookUserId = [[NSString alloc]initWithString:strFBID];
break;
}
default:
break;
}
}];
}
这在我的行动方法中
-(IBAction)Facebook_Btn_Clicked:(id)sender{
[appDelegate openSessionWithAllowLoginUI:YES];
}