简体   繁体   English

使用Swift崩溃时Facebook登录中的取消按钮崩溃?

[英]Cancel button in Facebook Login using Swift crashes?

I just followed a tutorial to make a Facebook login button using swift and parse. 我只是按照一个教程使用swift和parse制作了一个Facebook登录按钮。 I pushed the login button and logged in successfully for the first time. 我按下了登录按钮,并首次成功登录。 After, that, when I run the app again and push the log-in button again, I get the screen in the following image: login page 之后,当我再次运行该应用程序并再次按“登录”按钮时,我得到下图的屏幕: 登录页面

When I push OK everything is fine and I go to next viewContoller, the problem is after I push the Cancel button, app crashes. 当我按“确定”时,一切都很好,然后转到下一个viewContoller,问题出在我按“取消”按钮后,应用程序崩溃了。 Could you please give me some hints why it happens? 你能给我一些提示为什么会发生吗?

import UIKit
import Parse
import ParseFacebookUtilsV4
import ParseTwitterUtils
var userName: String = ""
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @available(iOS 8.0, *)
    @IBAction func signInButtonTapped(sender: AnyObject) {

        PFFacebookUtils.logInInBackgroundWithReadPermissions([], block: { (user: PFUser?, error: NSError?) -> Void in

            if (error != nil)
            {
                //Display an alert message
                var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)

                myAlert.addAction(okAction);

                self.presentViewController(myAlert, animated: true, completion: nil)

                return
            }

            if(FBSDKAccessToken.currentAccessToken() != nil) {
                userName = (PFUser.currentUser()?.objectId)!

                let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
                let protectedPageNav = UINavigationController(rootViewController: protectedPage)
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                var window = UIApplication.sharedApplication().keyWindow
                    window?.rootViewController = protectedPageNav
            }
        })   
    }
}

During OK or Cancel that still present in the PFFacebookUtils.logInInBackgroundWithReadPermissions block, when you hit it there is no problem because it calls 在“确定”或“取消”期间,PFFacebookUtils.logInInBackgroundWithReadPermissions块中仍然存在,单击时没有问题,因为它会调用

if (error != nil)
            { }

When you click "Cancel" below method may be call , 当您单击“取消”时,下面的方法可能会被调用,

 if(FBSDKAccessToken.currentAccessToken() != nil) { }

Because the token already valid, check it out! 由于令牌已经有效,请签出!

Here i used the Facebook Login through the objective c code, You can get the point here, 在这里,我通过目标C代码使用了Facebook Login,您可以在这里找到要点,

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
      [login
       logInWithReadPermissions:  @[@"public_profile",@"email",@"user_friends"]
       fromViewController:self
       handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                   STOP_LOAD;
                   NSLog(@"Process error");
                   NSLog(@"%@",error);
                   TOAST_FOR_TRY_AGAIN;
                   /*
                   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Please Try Again"
                                                                 message:nil
                                                                delegate:self
                                                       cancelButtonTitle:@"Ok"
                                                       otherButtonTitles: nil];
                   [alert show];
                    */
             } else if (result.isCancelled)
             {
                    STOP_LOAD;
                   NSLog(@"Cancelled");
             } else
             {
                   NSLog(@"Logged in");
                   NSLog(@"Result=%@",result);

                   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields": @"id,first_name,middle_name,last_name,name,picture,email"}]
                    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
                    {
                          NSLog(@"Facebook result=%@",result);
                         if (!error)
                          {


                          } else {

                                NSLog(@"An error occurred getting friends: %@", [error localizedDescription]);
                          }
                    }];
             }
       }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM