Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS/Swift:PFFacebookUtils.logInWithPermissions返回nil user和error_Ios_Facebook_Authentication_Swift_Parse Platform - Fatal编程技术网

iOS/Swift:PFFacebookUtils.logInWithPermissions返回nil user和error

iOS/Swift:PFFacebookUtils.logInWithPermissions返回nil user和error,ios,facebook,authentication,swift,parse-platform,Ios,Facebook,Authentication,Swift,Parse Platform,在我的应用程序中,我通过Parse的PFFacebookUtil类登录用户。如果手机上存在该用户(即在设置>Facebook中登录FB),则一切正常 但是,如果用户未通过设置登录,则用户将被带到web视图进行登录。用户输入凭据后,返回块应接收用户或错误,但在这种情况下,用户和错误均为零 let permissionsArray = ["user_about_me", "email"]; PFFacebookUtils.logInWithPermissions(permission

在我的应用程序中,我通过Parse的PFFacebookUtil类登录用户。如果手机上存在该用户(即在设置>Facebook中登录FB),则一切正常

但是,如果用户未通过设置登录,则用户将被带到web视图进行登录。用户输入凭据后,返回块应接收用户或错误,但在这种情况下,用户和错误均为零

    let permissionsArray = ["user_about_me", "email"];
    PFFacebookUtils.logInWithPermissions(permissionsArray, block: {
        (user: PFUser!, error: NSError!) -> Void in
        if user != nil {
              //successful login
        } else if error != nil{
              //unsuccessful login 
        } else {
              //this is what I get
        }
    }

我们目前正在运行Parse 1.4.2。目前,我看不到您的代码有任何问题。但根据我的经验,我会检查空
NSError
,而不是
PFUser

这是我在应用程序中使用Facebook登录和解析的解决方案,它是在objective-c中完成的,但我相信它会让您走上正确的道路。。。在代码中设置断点,并检查
PFUser
NSError
或log
PFUser
NSError
的值。也可以访问
Parse.com
,查看他们的Facebook登录解决方案。在
Objective-C
中有一个示例应用程序

[PFFacebookUtils logInWithPermissions:@[@"public_profile", @"email"] block:^(PFUser *user, NSError *error) {
    if (error) {
        [CBUtility facebookErrorHandler:error];
        return;
    } else {
        if (![CBUtility userHasValidFacebookData:user]) {
            [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (error) {
                    [CBUtility facebookErrorHandler:error];
                    return;
                }

                NSDictionary *user = (NSDictionary *)result;

                if (!user[@"email"]) {
                    [[[UIAlertView alloc] initWithTitle:@"Facebook Login/Register Error"
                                                message:[NSString stringWithFormat:@"There is no email associated with the current facebook account. The Registration could not proceed! Either login to your facebook account and create one or create a new crossbook account in the Register view."]
                                               delegate:nil
                                      cancelButtonTitle:nil
                                      otherButtonTitles:@"Dismiss", nil] show];
                    [[PFUser currentUser] delete];
                    return;
                }

                [PFUser currentUser][kCBUserFirstNameKey] = user[@"first_name"];
                [PFUser currentUser][kCBUserLastNameKey] = user[@"last_name"];
                [PFUser currentUser][kCBUserDisplayNameKey] = user[@"name"];
                [[PFUser currentUser] setEmail:user[@"email"]];
                [PFUser currentUser][kCBUserGenderKey] = [user[@"gender"] capitalizedString];
                [PFUser currentUser][kCBUserFacebookIDKey] = user[@"id"];

                if ([user[@"birthday"] length] != 0) {
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
                    NSDate *date = [dateFormatter dateFromString:user[@"birthday"]];
                    NSDateFormatter *stringFormatter = [[NSDateFormatter alloc] init];
                    [stringFormatter setDateFormat:@"MMM dd, yyyy"];
                    [PFUser currentUser][kCBUserBirthdateKey] = [stringFormatter stringFromDate:date];
                }

                [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {


                    if ([error code] == 203) {
                        [[[UIAlertView alloc] initWithTitle:@"Facebook Login/Register Error"
                                                    message:[NSString stringWithFormat:@"Apparently, the email address %@ has already been taken. Login with the username and password that is associated with the email address %@ and link accounts to allow facebook login by going to settings and link facebook account. If you forgot the password for the account, press 'Forgot Password' and type in the email address.", user[@"email"], user[@"email"]]
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Dismiss", nil] show];
                        [[PFUser currentUser] delete];
                        return;
                    }

                    if (error) {

                        [[[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:[error localizedDescription]
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Dismiss", nil] show];
                        [PFUser logOut];
                        return;
                    }

                    [[NSNotificationCenter defaultCenter] postNotificationName:CBLoginRegisterControllerUserDidFinishLoginNotification object:nil];
                    [self dismissViewControllerAnimated:YES completion:nil];
                    [(AppDelegate *)[[UIApplication sharedApplication] delegate] downloadFacebookPhoto];
                    return;
                }];
            }];
            return;
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:CBLoginRegisterControllerUserDidFinishLoginNotification object:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
        [(AppDelegate *)[[UIApplication sharedApplication] delegate] downloadFacebookPhoto];
        return;
    }
}];

问题是,我没有在AppDelegate调用中调用FBAppCall.handleOpenURL():

application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) 
当我从一个网络作者那里回来的时候。通过不调用FBAppCall.handleOpenURL(),Parse认为我们取消了身份验证。解析文档声明“如果用户通过切换回应用程序取消了身份验证,那么user和error都是nil。”

此方法应调用FBAppCall.handleOpenURL以将身份验证传递回应用程序。在我的例子中,我也使用这个调用进行深度链接,但我没有正确处理逻辑。我最初是在检查sourceApplication对象,看它是否是“com.facebook.facebook”。如果返回true,那么我调用了FBAppCall.handleOpenURL()。当我今天调试它时,我注意到源应用程序实际上是“com.apple.mobilesafari”无论如何,检查sourceApplication并不是最好的选择(试试url.host之类的东西),但在这种情况下,这就是问题所在。

下面是固定的代码片段:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) {
    if (url.host == DEEP_LINKING_HOST) {
       //Deep linking code here...
    } else if sourceApplication == "com.apple.mobilesafari" {
       return FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication, withSession: PFFacebookUtils.session())
   }
}

这里的要点是我没有调用FBAppCall.handleopeanurl()。结果,应用程序认为我取消了登录,并给了我一个零用户和错误。

我使用的是Facebook SDK的版本
4.10.1
。在此版本中,没有
FBAppCall
类。改为使用
FBSDKApplicationDelegate
,并将此代码段粘贴到应用程序委托中

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL canOpen = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];

    return canOpen;
}

“你找到解决办法了吗?”马沙迪在下面看到了我的答案。希望有帮助。我已经在我的应用程序代理中使用了这个,但我仍然有这个问题。如果用户尝试第二次登录,那么它工作得很好。哦,天哪,这是调试时间的来源。我所做的只是我的“handleOpenURL”方法没有“withSession”参数。解析文档在这方面具有误导性。