使用Facebook iOS SDK 3.1。
当选择不允许“连接”到Facebook应用程序时,我会被Facebook投掷“com.facebook.sdk error2”。即使在重新安装我的应用程序后,我也会遇到错误。
重现的步骤:
UIAlertView
中选择“Not Now”=>我不能再选择连接了。
用户再次连接的唯一方法是从“设置”中删除她的Facebook帐户并重新添加。
这是bug in the Facebook SDK还是我错过了什么?
我明显遵循授权教程,在选择连接时一切正常(auth,发布内容)。
答案 0 :(得分:10)
好的,所以我想出了这里发生了什么。拒绝授权应用程序时,会将其存储为设备的Facebook帐户(设置> Facebook)上的设置。
通过转到“设置”并重新启用相关应用,您可以尝试再次连接。用户不太清楚,但您可以捕获此错误并向用户显示某种信息。
这就是我实现它的方式(与Facebook's default error handling相比):
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error {
....
if (error) {
NSString *errorTitle = NSLocalizedString(@"Error", @"Facebook connect");
NSString *errorMessage = [error localizedDescription];
if (error.code == FBErrorLoginFailedOrCancelled) {
errorTitle = NSLocalizedString(@"Facebook Login Failed", @"Facebook Connect");
errorMessage = NSLocalizedString(@"Make sure you've allowed My App to use Facebook in Settings > Facebook.", @"Facebook connect");
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorTitle
message:errorMessage
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"Facebook Connect")
otherButtonTitles:nil];
[alertView show];
}
}
答案 1 :(得分:1)
当我在设置中添加帐户时尝试使用Facebook框架登录Facebook时,我遇到了同样的问题,但我使用以下代码修复了此问题:
尽我所知“com.facebook.sdk error2。”当iphone中添加了一个帐户时会出现。
appdelegate.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];
}