我在iOS中实施Facebook登录时遇到了一些麻烦,如果用户尚未登录,一切正常,应用程序正确地从Facebook获取数据并将其传递给下一个ViewController,如果是已登录的应自动转到显示用户信息的回顾页面,但我无法实现,目前我正在使用此方法:
override func viewDidLoad() {
super.viewDidLoad()
LoginButton.delegate = self
if (FBSDKAccessToken.currentAccessToken() != nil) {
self.performSegueWithIdentifier("Login", sender: self)
}
}
但在控制台我得到:
Facebook_Login.LoginViewController:Facebook_Login.ViewController上的0x7fc04a519ca0:0x7fc04a41c1e0,其视图不在窗口层次结构中!
我也尝试过使用viewdidAppear方法,但是它会在不更新变量的情况下进入回顾页面,因此我得到一个空页
这里'完整的代码:
查看控制器1
导入UIKit 导入FBSDKLoginKit
类ViewController:UIViewController,FBSDKLoginButtonDelegate {
var nome1:String = ""
var cognome1:String = ""
var email1:String = ""
var compleanno:String = ""
var città:String = ""
var genere:String = ""
var immagine_url:String = ""
@IBOutlet weak var LoginButton: FBSDKLoginButton!
@IBAction func LoginAction(sender: AnyObject) {
LoginButton.delegate = self
LoginButton.readPermissions = ["email"]
}
func FetchInfo(){
print("scarico le informazioni...")
let parametri = ["fields":"email, first_name, last_name, birthday, hometown, gender, picture.type(large)"]
FBSDKGraphRequest(graphPath: "me", parameters: parametri).startWithCompletionHandler{(connection,result,error) -> Void in
if (error != nil){
print ("errore")
return
}
else {
if let email = result["email"] as? String {
print(email)
self.email1 = email
}
if let nome = result["first_name"] as? String {
print(nome)
self.nome1 = nome
}
if let cognome = result["last_name"] as? String {
print(cognome)
self.cognome1 = cognome
}
if let compleanno = result["birthday"] as? String{
print(compleanno)
}
if let città = result["hometown"] as? String{
print(città)
}
if var genere = result["gender"] as? String{
if (genere == "male"){
genere = "maschio"
}
else {
genere = "femmina"
}
print(genere)
}
}
if let picture = result["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, url = data["url"] as? String{
self.immagine_url = url
print(self.immagine_url)
}
}
return
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){
if (result.isCancelled == true){
print("cancellato")
}
else {
print("login effettuato")
FetchInfo()
self.performSegueWithIdentifier("Login", sender: self)
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!){
}
func loginButtonWillLogin(loginButton: FBSDKLoginButton!) -> Bool{
return true
}
@IBAction func returned(segue:UIStoryboardSegue){
}
override func viewDidLoad() {
super.viewDidLoad()
LoginButton.delegate = self
}
override func viewDidAppear(animated: Bool) {
if (FBSDKAccessToken.currentAccessToken() != nil) {
FetchInfo()
if(nome1 == ""){
}
else {
self.performSegueWithIdentifier("Login", sender: self)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinazione:LoginViewController = segue.destinationViewController
as! LoginViewController
destinazione.temp_nome = nome1
destinazione.temp_cognome = cognome1
destinazione.temp_email = email1
destinazione.img_profile_url = immagine_url
}
}
ViewController 2:
导入UIKit 导入FBSDKLoginKit
类LoginViewController:UIViewController,FBSDKLoginButtonDelegate {
var temp_nome = ""
var temp_cognome = ""
var temp_email = ""
var img_profile_url:String = ""
@IBOutlet weak var Nome_Utente: UILabel!
@IBOutlet weak var email: UILabel!
@IBOutlet weak var Immagine_Utente: UIImageView!
@IBOutlet weak var Login_button: FBSDKLoginButton!
@IBAction func Login_button_Action(sender: AnyObject) {
Login_button.delegate = self
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!){
self.performSegueWithIdentifier("Back", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
Nome_Utente.text = "\(temp_nome)" + " " + "\(temp_cognome)"
email.text = "\(temp_email)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:0)
首先,请避免重置LoginButton.delegate
功能中的LoginAction()
。
如果用户已经登录,为了避免对Facebook Graph API的无用调用(如果您不一定需要更新他的信息),您可以将您的用户信息存储在 CoreData 或在 NSUserDefault 。
如果您从未使用它,可以使用CDHelper lib(CoreDataHelper),这样您就可以毫无困难地使用它。
希望我有所帮助,如果没有,请不要犹豫,给我们一个反馈。
顺便说一句,在您的viewDidAppear(animated: Bool)
函数中,您必须致电super.viewDidAppear(animated)
!