2015-12-22 16 views
1

我剛剛按照教程做了一個使用swift和parse的Facebook登錄按鈕。我推入登錄按鈕並首次成功登錄。之後,那個,當我再次運行應用程序,並再次按下登錄鍵,我得到了畫面如下圖: login pageFacebook中的取消按鈕使用Swift崩潰?

當我按下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 
      } 
     }) 
    } 
} 

回答

4

在確定或取消,仍然存在於PFFacebookUtils.logInInBackgroundWithReadPermissions塊,當你打它是沒有問題的,因爲它要求

if (error != nil) 
      { } 

當您單擊「取消」的方法,可以調用,

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

因爲令牌已經有效,請檢查一下!

+0

所以,如果我沒有記錯你的建議,與其如果(FBSDKAccessToken.currentAccessToken() != if(FBSDKAccessToken.currentAccessToken()!= nil){} else {},然後在else部分將用戶重定向到應用程序的第一頁? – lollipop

+0

實際上,如果用戶取消,Facebook登錄功能可以選擇。這應該被稱爲。我在我的下一個答案上顯示我的代碼。 –

+0

非常感謝。是的,請好好做。 – lollipop

0

在這裏,我使用Facebook登錄通過客觀的C代碼,你可以在這裏點,

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