iOS Facebook登录无效

时间:2013-06-03 09:38:01

标签: ios facebook facebook-ios-sdk

我有一个应用程序,适用于iOS5及更高版本,我正在尝试设置Facebook登录。当手机中集成了Facebook帐户时,一切正常。如果不是,则不起作用。即使安装了Facebook应用程序也是如此。

安装应用程序后,它会打开应用程序,询问我是否有权访问相应的用户数据并返回我的应用程序而没有结果。它没有安装时也一样。 Safari已打开,要求我授权,退货,什么都没有。

我按照Facebook登录教程中提到的所有步骤进行操作。这只是我面临的一个问题吗?

编辑:这是请求的代码:

我打开一个这样的会话:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

    NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"email",
                        nil];

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState state,
                                                         NSError *error) {
                                         [self sessionStateChanged:session
                                                             state:state
                                                             error:error];
                                     }];
}

当它返回时,我想这个方法必须被调用。但事实并非如此:

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{

    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"session opened");
                FBRequest *me = [FBRequest requestForMe];
                __block NSString *username = nil;

                [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                             id result,
                                             NSError *error) {
                    NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
                  //  NSLog(@"User session found: %@", my.username);
                    username = my.username;
                }];

            // /fb_access/:access_token

        }
        break;
    case FBSessionStateClosed:

        if (!error) {
            // We have a valid session
            NSLog(@"User session closed");

        }

    case FBSessionStateClosedLoginFailed:
         NSLog(@"login failed, %@", error);
        [FBSession.activeSession closeAndClearTokenInformation];
        break;
    default:
        break;
}

[[NSNotificationCenter defaultCenter]
 postNotificationName:FBSessionStateChangedNotification
 object:session];

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:error.localizedDescription
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];
}
}

有什么想法吗?

3 个答案:

答案 0 :(得分:4)

在plist中添加以下键值对。

  1. 添加字段(键)FacebookAppID - (值)fbID
  2. 添加字段 - 网址类型(键)
  3. 在网址类型中添加2个字段:
    • (键)URL标识符 - (值)(即YourApplication名称)
    • (关键)-URL计划
  4. 在网址方案中添加字段(键) - (值)fb(fbID)(例如fb557354324264278)

答案 1 :(得分:2)

将以下方法添加到您的appdelgate

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

 { // attempt to extract a token from the url return

 [FBSession.activeSession handleOpenURL:url]; 

 }

答案 2 :(得分:0)

iUser和Kalpesh的答案都是正确的。两者结合起来对我有用。请参考他们。 :)

相关问题