我需要在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
}
}
您可以将数据发送到其他视图的代码和他们后来又保存数据,访问它的任何地方... –
怎么能我获得相关的?这种情况下令牌?我怎样才能做到这一点?我知道在迅速:/ –