PFFacebookUtils logInWithPermissions在模拟器上失败

时间:2013-12-17 10:41:55

标签: ios facebook ios7 parse-platform facebook-sdk-3.0

我正在将我的iOS应用与Parse和Facebook集成。它在我的iPhone 4s上工作正常 - 运行iOS 6 - 但是当我在我的xCode 5模拟器上尝试测试帐户时它失败了。

当我用facebook按钮登录时,我看到一个屏幕说:你已经授权Test999(我的应用程序的名字。)当我打电话给这一行时,就会发生这种情况:

[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) 

我按OK。它将我发送到“您必须先登录”屏幕。我输入了我的用户名和密码,然后按下登录状态 - 我将被发送回您已经授权的Test999。我按OK - 我看到一个空白视图,左上方有一个x来关闭它。没有别的事可做,所以我推它,我被送回“你必须先登录”屏幕。

所以真的没办法用模拟器登录。我试图“重置内容和设置”,但它没有帮助。

在回答类似问题之后,我尝试添加reconnectWithFb方法(下面),我还将我的Facebook sdk更新到3.9版。什么都没有帮助。

这是我用facebook方法登录的:

[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {

if (!user) {
    if (!error) {
        NSLog(@"Uh oh. The user cancelled the Facebook login.");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"User cancelled facebook login" message:@"Uh oh. The user cancelled the Facebook login." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
        [alert show];
    } else {
        NSLog(@"welcome screen loginWithFB: Uh oh. An error occurred: %@", error);
        [self performSelector:@selector(reconnectWithFB) withObject:nil afterDelay:0.5];
    }
} else if (user.isNew) {
    NSLog(@"New user with facebook signed up and logged in!");
} else {
    NSLog(@"User with facebook logged in!");        
    FBRequest *request = [FBRequest requestForMe];
    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSString *facebookUsername = [result objectForKey:@"name"];
            [PFUser currentUser].username = facebookUsername;
            [[PFUser currentUser] setObject:[result objectForKey:@"id"] forKey:@"facebookid"];
            [[PFUser currentUser] saveEventually];
            [[NSNotificationCenter defaultCenter] postNotificationName:kUsersToWatchNotification object:result];
        } else {
            NSLog(@"Error getting the FB username %@", [error description]);
        }
    }];
}

和我的reconnectWithFB(在模拟器上总是有一个空的fbAccounts数组:)

-(void) reconnectWithFB {
    NSLog(@"reconnectWithFB called");
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore  accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){
    NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];

    NSLog(@"fbAccounts %@", fbAccounts);

    id account;
    if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){
        [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
                //we don't actually need to inspect renewResult or error.
                if (error){
                    NSLog(@"error in reconnectWithFB %@", error);
                }
        }];
      } 
   }
}

对此的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:5)

看起来解决方案是将open url方法添加到我的App Delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [PFFacebookUtils handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url     sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [PFFacebookUtils handleOpenURL:url];
}

答案 1 :(得分:1)

这个答案似乎已经过时,编译器警告不要使用。

当前的方法应该是:

- (BOOL) handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [FBAppCall handleOpenURL:url
                      sourceApplication:sourceApplication
                      withSession:[PFFacebookUtils session]];
}

答案 2 :(得分:1)

更新方式:

在您的AppDelegate.m中输入以下内容:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [FBAppCall handleOpenURL:url
                  sourceApplication:sourceApplication
                        withSession:[PFFacebookUtils session]];
}

然后请务必致电:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
        [FBAppEvents activateApp]; 
}

稍后在编码中确保添加此单行:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[PFFacebookUtils session] close];
}
相关问题