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

时间:2015-12-22 05:53:47

标签: ios facebook swift parse-platform

我只是按照教程使用swift和parse制作Facebook登录按钮。我按下登录按钮并第一次成功登录。之后,当我再次运行应用程序并再次按下登录按钮时,我会看到以下图像中的屏幕: login page

当我推OK一切都很好,我去下一个viewContoller,问题是我按下取消按钮,应用程序崩溃。你能不能给我一些提示,为什么会这样?

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
            }
        })   
    }
}

2 个答案:

答案 0 :(得分:5)

在PFFacebookUtils.logInInBackgroundWithReadPermissions块中仍然存在的OK或Cancel期间,当你点击它时没有问题,因为它调用

eval

点击"取消"下面的方法可能是打电话,

if (error != nil)
            { }

因为令牌已经有效,请查看它!

答案 1 :(得分:0)

在这里,我通过目标c代码使用Facebook登录,你可以在这里说明一点,

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]);
                          }
                    }];
             }
       }];