我剛剛按照教程做了一個使用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
}
})
}
}
所以,如果我沒有記錯你的建議,與其如果(FBSDKAccessToken.currentAccessToken() != if(FBSDKAccessToken.currentAccessToken()!= nil){} else {},然後在else部分將用戶重定向到應用程序的第一頁? – lollipop
實際上,如果用戶取消,Facebook登錄功能可以選擇。這應該被稱爲。我在我的下一個答案上顯示我的代碼。 –
非常感謝。是的,請好好做。 – lollipop