2015-04-04 43 views
2
import Foundation 
import Alamofire 
import UIKit 
import SwiftyJSON 

class APIClient: UIViewController { 

    let loggedIn = false 
    struct Constants{ 
     static let Domain = "http://gogogo.com" 
    } 

    func authenticate() -> Bool{ 
     println("Authenticating") 
     if let access_token = FBSDKAccessToken.currentAccessToken().tokenString { 
      let parameters = [ "access_token": access_token ] 
      Alamofire.request(.POST, Constants.Domain+"/accounts", parameters: parameters).responseJSON { 
      (req, res, json, error) in 
       if(error != nil) { 
        self.sendReturnToSplash() 
       } else { 
        let json = JSON(json!) 
        println("\(json)") 
        return true //This doesn't work!!!!!! 
       } 
      } 
     }else{ 
      println("No access token to authenticate") 
     } 
    } 

    func sendReturnToSplash(){ 
     let center = NSNotificationCenter.defaultCenter() 
     let notification = NSNotification(name: NotificationCenters.ReturnToSplash, object: self, userInfo: ["":""]) 
     center.postNotification(notification) 
    } 
} 

正如你可以看到,如果注销成功,我想回到“真”。但是,XCode中给了我一个既然你使用的是异步调用“太虚不符合协议BooleanLiteral”如何从AlamoFire返回的东西?

+0

使用函数的完成块来查看注销是否成功。 – ad121 2015-04-05 00:18:55

回答

3

,你应该把你的身份验证功能异步。我建议使用类似于以下的完成块:

func authenticate(completion:(success: Bool) -> Void) { 
    println("Authenticating") 
    if let access_token = FBSDKAccessToken.currentAccessToken().tokenString { 
     let parameters = [ "access_token": access_token ] 
     Alamofire.request(.POST, Constants.Domain+"/accounts", parameters: parameters).responseJSON { (req, res, json, error) in 
      if error != nil { 
       self.sendReturnToSplash() 
       completion(success: false) 
      } else { 
       let json = JSON(json!) 
       println("\(json)") 
       completion(success: true) 
      } 
     } 
    } else { 
     println("No access token to authenticate") 
     completion(success: false) 
    } 
}