iOS - Facebook SDK - SSO - 无需登录对话框登录

时间:2014-02-04 10:02:54

标签: ios iphone facebook-login facebook-sdk-3.1

我在运行时通过以下方式更改Info.plist文件:

-(void) setBundleUrlScheme:(NSString*)fbAppId {

    NSString *path= [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];

    if(path){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
        NSMutableArray* bundleUrlTypesArray = [dict objectForKey:@"CFBundleURLTypes"];
        NSMutableDictionary* bundleURLSchemes = [bundleUrlTypesArray objectAtIndex:0];

        // set wanted fb app id to plist CFBundleURLSchemes
        [bundleURLSchemes setObject:[NSArray arrayWithObject:[NSString stringWithFormat:@"fb%@", fbAppId]] forKey:@"CFBundleURLSchemes"];

        // write back to file
        [dict writeToFile:path atomically:YES];

}

}

之后,我尝试登录到没有登录对话框的Facebook(我的应用程序允许SSO):

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                   allowLoginUI:NO
                                 pletionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

            // error occured
            if(error || state == FBSessionStateClosedLoginFailed) {
                NSLog(@"ERROR:%@", error);
            }

            // session opened succefully, run query
            else if (state == FBSessionStateOpen){

                NSLog(@"state:%d", state);
                NSLog(@"I LOGGED IN!");

                sucessBlock();
            }
        }];

每次我在allowLoginUI中传递NO时,openActiveSessionWithReadPermissions都会返回NO,而成功/失败块永远不会运行。

如何在没有登录用户界面的情况下登录Facebook?

提前致谢。

1 个答案:

答案 0 :(得分:1)

代码的第一部分不起作用,因为它只是在设备上读取,所以无法写入mainBundle。

秒部分可能会失败,因为代码的第一部分失败了。

如果文件是实际写的,只需在代码中添加此检查:

// write back to file
if (![dict writeToFile:path atomically:YES]) {
   NSLog:(@"Failed to write: %@", path);
}