Axo*_*ort 10 facebook objective-c ios facebook-ios-sdk
我想在我的应用程序中加入一些Facebook集成.在这一点上,我已经设法登录,发布到朋友的墙上,检索朋友列表等等.除了一件事,一切都还好......
如果用户从您的Facebook设置/应用程序中删除应用程序 ,然后进入iOS应用程序,则代码无法识别Facebook应用程序已从用户设置中删除并假设已登录(这是问题,因为如果用户试图发布到朋友的墙上,应用程序什么都不做).
然后,用户关闭iOS应用程序并重新启动它...通过此重新启动,iOS应用程序"已修复"并检测到用户不再登录.
在用户从设置中删除Facebook应用程序之后,我无法立即检测到该时刻,以便将登录流程带给用户...
这是我的代码:
在我的应用程序的第一个场景......
if([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
{
NSLog(@"Logged in to Facebook");
[self openFacebookSession];
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc] initWithTitle:@"Facebook" message:@"You're already logged in to Facebook" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertDialog show];
[alertDialog release];
return YES;
}
else{
NSLog(@"Not logged in to Facebook"); //Show the login flow
return NO;
}
Run Code Online (Sandbox Code Playgroud)
这是openFacebookSession的代码
-(void)openFacebookSession
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"publish_stream",
nil];
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
}];
}
Run Code Online (Sandbox Code Playgroud)
sessionStateChanged的代码......
-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
NSLog(@"Session opened");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你!
小智 5
我发现这也发生在我自己身上......当用户更改了他们的Facebook密码时发现了,我们无法再进行身份验证.手动重新同步/清除帐户会话允许他们再次登录.
-(void)syncFacebookAccount
{
[self forceLogout];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"];
if (accountStore && accountTypeFB) {
NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
id account;
if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])) {
[accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
// Not actually using the completion handler...
}];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,并且在 Facebook SDK 网站上找不到有关错误代码的正确文档。
我通过比较[error code];或[error userInfo];值解决了问题。
您的会话将具有状态FBSessionStateClosedLoginFailed,错误的 userInfo 字典将具有以下形式
"com.facebook.sdk:ErrorLoginFailedReason" = "com.facebook.sdk:ErrorLoginFailedReason";
Run Code Online (Sandbox Code Playgroud)
另一方面,错误代码向我显示,2以便您可以在 sessionStateChanged::: 函数结束时处理它
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error {
switch (state) {
case FBSessionStateOpen: {
//update permissionsArrat
[self retrieveUSerPermissions];
if (!needstoReopenOldSession) {
//First User information
[self getUserInformation:nil];
}
NSNotification *authorizationNotification = [NSNotification notificationWithName:facebookAuthorizationNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];
}
case FBSessionStateClosed: {
break;
}
case FBSessionStateClosedLoginFailed: {
[FBSession.activeSession closeAndClearTokenInformation];
break;
}
default:
break;
}
if (error)
{
NSNotification *authorizationNotification = [NSNotification notificationWithName:faceBookErrorOccuredNotification object:error];
[[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8584 次 |
| 最近记录: |