2017-05-13 57 views
0

我需要在swift中创建一个新的应用程序,我使用公司API登录使用.post,但我怎么能沙到另一个视图?我需要在另一个视图访问。获得的认证......如何发送登录请求swift到另一个视图

我的登入码:

@IBOutlet weak var emailField: UITextField! 
@IBOutlet weak var passwordField: UITextField! 

@IBAction func loginButton(_ sender: UIButton) { 
    let emailText: String? = emailField.text 
    let passwordText: String? = passwordField.text 

    APIAuthentication.loginWith(email: emailText!, password: passwordText!) { (response, error, cache) in 

     if response != nil { 

     } else if let error = error { 
      if let urlResponse = error.urlResponse, urlResponse.statusCode == 401 { 
       // logout user 
      } else if let responseObject = error.responseObject as? [String: Any], let _ = responseObject["error_message"] { 
       // show errorMessage 
      } else { 
       // show error.originalError.localizedDescription 
      } 
     } 
    } 
} 

我的请求API

struct Constants { 
    static let baseURL = URL(string: "xxxx/api/v1/users/auth/")! 
    static let apiPath = "" 
    static let authenticationHeaders = ["access-token", "client", "uid"] 
    static let authenticationHeadersDefaultsKey = "authenticationHeaders" 
} 

APIAuthentication

struct User: Mappable { 
init(mapper: Mapper) { } 

}

class API验证:APIRequest {

@discardableResult 
static func loginWith(email: String, password: String, callback: ResponseBlock<User>?) -> APIAuthentication { 

    let request = APIAuthentication(method: .post, path: "sign_in", parameters: ["email": email, "password": password], urlParameters: nil, cacheOption: .networkOnly) { (response, error, cache) in 

     if let error = error { 
      callback?(nil, error, cache) 
     } else if let response = response as? [String: Any] { 
      let user = User(dictionary: response) 
      callback?(user, nil, cache) 
     } 
    } 
    request.shouldSaveInCache = false 
    request.makeRequest() 

    return request 
} 

}

+0

您可以将数据发送到其他视图的代码和他们后来又保存数据,访问它的任何地方... –

+0

怎么能我获得相关的?这种情况下令牌?我怎样才能做到这一点?我知道在迅速:/ –

回答

1

您可以用赛格瑞通过不同ViewControllers之间的数据。请尝试通过调用Segue公司,并通过有关数据或使一个Singleton对象实现以下

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let yourVc = segue.destination as? YourViewController 
    yourVc?.YOUR_VARIABLE = VariableWithDataFromCurrentVc 
} 
+0

然后调用performSegue(具有:标识符,发件人:任何?)函数来实现... –

+0

在这种情况下,VariableWithDataFromCurrentVc是我的令牌访问?但是,我如何存储它? –

+0

如果您需要保存数据,则可以使用NSUserDefaults。请接受我的回答:-)。 – Grumme

相关问题