Token not getting empty after logout swift - ios

i have a swift application which uses API for authentication everything works fine and when users logout, the login token is supposed to get cleared so that the new user's details can be collected and the new Token passed into the header but i noticed that when I try login in another user, the previous users token remains in the header and thereby preventing the new user from login in. I clear the login values on logout button pressed but i have no idea why the token value remains in the header. my codes are shown below
let defaults = UserDefaults.standard
var isLoggedIn : Bool {
get {
return defaults.bool(forKey: LOGGED_IN_KEY)
}
set {
defaults.set(newValue, forKey: LOGGED_IN_KEY)
}
}
var authToken: String {
get {
return defaults.value(forKey: TOKEN_KEY) as? String ?? ""
}
set {
defaults.set(newValue, forKey: TOKEN_KEY)
}
}
var userUsername: String {
get {
return defaults.value(forKey: USER_USERNAME) as? String ?? ""
}
set {
defaults.set(newValue, forKey: USER_USERNAME)
}
}
//MARK :- LOGGIN
func findUserByUserName(completion: #escaping CompletionHandler) -> Void {
Alamofire.request(URL_USER_BY_USERNAME, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: TOKEN_HEADER).validate().responseJSON { (response) in
print("URL USER BY HEADER \(self.authToken)")
if response.result.error == nil {
guard let data = response.data else {return}
let jsonString = String(data: data, encoding: .utf8)
print(jsonString as Any)
self.setUserInfo(data: data)
completion(true)
}
else {
completion(false)
debugPrint("ERROR 22222\(response.result.error as Any)")
}
}
}
func setUserInfo(data: Data) -> Void {
do {
let json = try JSON(data: data)
let pk = json["pk"].intValue
let username = json["username"].stringValue
let email = json["email"].stringValue
let firstName = json["first_name"].stringValue
let lastName = json["last_nameme"].stringValue
print("THE USERNAME IZZZZ \(username)")
UserDataService.instance.setUserData(pk: pk, username: username, email: email, firstName: firstName, lastName: lastName)
} catch {
print(error)
}
func loginUser(email: String, password: String, completion: #escaping CompletionHandler) -> Void {
let usernameEmail = email.lowercased()
let body: [String: Any] = [
"username": usernameEmail,
"email": "",
"password": password,
]
Alamofire.request(URL_LOGIN, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).validate().responseJSON { (response) in
if response.result.error == nil {
print("LOGIN SUCCESFULL \(self.authToken)")
do {
guard let data = response.data else {return}
let jsonString = String(data: data, encoding: .utf8)
print("HELLOOO \(jsonString as Any)")
let json = try JSON(data: data)
self.authToken = json["key"].stringValue
self.userUsername = email
self.isLoggedIn = true
completion(true)
print("LOGIN SUCCESFULL TOKEN1111 \(self.authToken)")
} catch {
print("errorrrrr")
}
} else {
completion(false)
debugPrint("ERROR YENNNNN \(response.result.error as Any)")
}
}
}
//MARK :- LOGGIN
func findUserByEmail(completion: #escaping CompletionHandler) -> Void {
let body: [String: Any] = [
"username": AuthService.instance.userUsername,
]
Alamofire.request(URL_USER_BY_EMAIL, method: .put, parameters: body, encoding: JSONEncoding.default, headers: TOKEN_HEADER).validate().responseJSON { (response) in
print("URL USER BY HEADER \(self.authToken)")
if response.result.error == nil {
guard let data = response.data else {return}
print("USERUSERNAME \(self.authToken)")
let jsonString = String(data: data, encoding: .utf8)
print(jsonString as Any)
self.setUserInfo(data: data)
completion(true)
}
else {
completion(false)
debugPrint("ERROR 22222\(response.result.error as Any)")
}
}
}
Token Constant
let TOKEN_HEADER = [
"Authorization": "Token \(AuthService.instance.authToken)",
"Content-Type": "application/json; charset=utf-8"
]
UserService
func setUserData(pk: Int, username: String, email: String, firstName: String, lastName: String) -> Void {
self.pk = pk
self.username = username
self.email = email
self.firstName = firstName
self.lastName = lastName
}
func logoutUser() -> Void {
self.pk = 0
self.username = ""
self.email = ""
self.firstName = ""
self.lastName = ""
AuthService.instance.isLoggedIn = false
AuthService.instance.authToken = ""
AuthService.instance.userUsername = ""
}
Logout
#IBAction func logoutPressed(_ sender: Any) {
UserDataService.instance.logoutUser()
print("LOGOUT TOKEN \(AuthService.instance.authToken)")
UserDataService.instance.setUserData(pk: 0, username: "", email: "", firstName: "", lastName: "")
AuthService.instance.authToken = ""
NotificationCenter.default.post(name: NOTIFY_USER_DATA_DID_CHANGE, object: nil)
dismiss(animated: true, completion: nil)
}
further codes would be supplied on request

The problem is you think that whenever you call TOKEN_HEADER you get the lastest value from
let TOKEN_HEADER = [
"Authorization": "Token \(AuthService.instance.authToken)",
"Content-Type": "application/json; charset=utf-8"
]
but this doesn't happen as the variable get it's value from first initialization whatever the token value was , so you have to refactor sending the the header to Alamofire , by hardcoding the string again like this
func findUserByUserName(completion: #escaping CompletionHandler) -> Void {
let updated_HEADER = [
"Authorization": "Token \(AuthService.instance.authToken)",
"Content-Type": "application/json; charset=utf-8"
]
Alamofire.request(URL_USER_BY_USERNAME,
method: .get, parameters: nil, encoding: JSONEncoding.default,
headers:updated_HEADER).validate().responseJSON { (response) in }
}

Related

Getting statusCode other than 200...299 in HTTPURLResponse of URLSession

the following is my APIManager code, I'm using it in all my apps. But sometimes, the guard statement fails in connectToServer function, which means the statusCode of HTTPURLResponse other than 200...299 and the thing here is even after getting statusCode other than 200...299 my record got inserted into DB. I don't know what happens.
I thought that the cause of this behavior is from ServerURL, because I'm using a dev server with IP address http://00.000.0.000/ without security. Once I moved it to domain as https://XXX.XXXXXXXXXX.XXXXX/ it is working fine. Can you help me to figure out this?
And also will it supports for asynchronous calls?
import UIKit
struct APIResponse : Decodable {
let status : Bool
let message : String
let extra: String?
}
internal let BASE_URL = "http://00.000.0.00/app/v0_1/api/" // Example server URL
enum APIPath: String {
case registration = "registration"
case login = "login"
case getProfile = "get_profile"
func directURL() -> URL? {
let urlPath = BASE_URL + self.rawValue
return URL(string: urlPath)
}
func extendedURL(using parameters: [String: Any]) -> URL? {
let extendedPath = parameters.map { $0.key + "=" + "\($0.value)" }.joined(separator: "&")
let urlPath = BASE_URL + self.rawValue + "?" + extendedPath
return URL(string: urlPath)
}
}
enum APIMethod: String {
case get = "GET"
case put = "PUT"
case post = "POST"
case patch = "PATCH"
case delete = "DELETE"
}
enum APIHeaders {
case user
case app
var authorization: [String:String] {
let acceptLanguage = UserDefaults.standard.value(forKey: UDKeys.appleLanguage) as? String ?? ""
if self == .user {
let token = UserDefaults.standard.value(forKey: UDKeys.userToken) as? String ?? ""
return ["Content-Type": "application/json", "Accept": "application/json", "Accept-Language": acceptLanguage, "Token" : token]
}
return ["Content-Type": "application/json", "Accept": "application/json", "Accept-Language": acceptLanguage]
}
}
struct APIRequest {
var url: URL?
var method: String
var parameters: Data?
var headers: [String:String]
init(path: APIPath, method: APIMethod, headers: APIHeaders) {
self.url = path.directURL()
self.method = method.rawValue
self.headers = headers.authorization
}
init(path: APIPath, parameters: [String: Any], method: APIMethod, headers: APIHeaders) {
self.url = path.extendedURL(using: parameters)
self.method = method.rawValue
self.headers = headers.authorization
}
init(path: APIPath, method: APIMethod, body: [String:Any], headers: APIHeaders) {
self.url = path.directURL()
self.method = method.rawValue
self.parameters = try? JSONSerialization.data(withJSONObject: body, options: .sortedKeys)
self.headers = headers.authorization
}
init<Encode: Encodable>(path: APIPath, method: APIMethod, body: Encode, headers: APIHeaders) {
self.url = path.directURL()
self.method = method.rawValue
self.parameters = try? JSONEncoder().encode(body)
self.headers = headers.authorization
}
}
struct APIError: Error {
let reason: String
let code: String?
init(reason: String, code: String? = nil) {
self.reason = reason
self.code = code
}
}
struct APIDispatcher {
static let instance = APIDispatcher()
private init() {}
func dispatch<Decode: Decodable>(request: APIRequest, response: Decode.Type, result: #escaping (Result<Decode, APIError>) -> ()) {
DispatchQueue(label: "queue", attributes: .concurrent).async {
self.connectToServer(with: request) { (resultant) in
switch resultant {
case .success(let data):
do {
let decoded = try JSONDecoder().decode(response, from: data)
DispatchQueue.main.async {
result(.success(decoded))
}
} catch let decodedError {
print("[Decoded Error]: ", decodedError)
do {
let apiResponse = try JSONDecoder().decode(APIResponse.self, from: data)
let apiError = APIError(reason: apiResponse.message, code: apiResponse.extra)
DispatchQueue.main.async {
result(.failure(apiError))
}
} catch {
let apiError = APIError(reason: decodedError.localizedDescription)
DispatchQueue.main.async {
result(.failure(apiError))
}
}
}
case .failure(let error):
DispatchQueue.main.async {
result(.failure(error))
}
}
}
}
}
func dispatch(request: APIRequest, result: #escaping (Result<Dictionary<String,Any>, APIError>) -> ()) {
DispatchQueue(label: "queue", attributes: .concurrent).async {
self.connectToServer(with: request) { (resultant) in
switch resultant {
case .success(let data):
do {
let serialized = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! Dictionary<String,Any>
DispatchQueue.main.async {
result(.success(serialized))
}
} catch {
let error = APIError(reason: error.localizedDescription)
DispatchQueue.main.async {
result(.failure(error))
}
}
case .failure(let error):
DispatchQueue.main.async {
result(.failure(error))
}
}
}
}
}
private func connectToServer(with request: APIRequest, result: #escaping (Result<Data, APIError>) -> ()) {
guard let url = request.url else {
let error = APIError(reason: "Invalid URL")
result(.failure(error))
return
}
var urlRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
urlRequest.httpMethod = request.method
urlRequest.httpBody = request.parameters
urlRequest.allHTTPHeaderFields = request.headers
print(urlRequest)
let urlSessionConfiguration = URLSessionConfiguration.default
urlSessionConfiguration.waitsForConnectivity = false
urlSessionConfiguration.timeoutIntervalForRequest = 30
urlSessionConfiguration.timeoutIntervalForResource = 60
let urlSession = URLSession(configuration: urlSessionConfiguration)
urlSession.dataTask(with: urlRequest) { (data, response, error) in
if let error = error {
let error = APIError(reason: error.localizedDescription)
result(.failure(error))
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
let error = APIError(reason: "Server Error")
result(.failure(error))
return
}
if let data = data {
result(.success(data))
}
}.resume()
}
}
Note: BASE_URL and APIResponse might be vary according to project.
I'm using it as
func login() {
self.startLoading()
let body = ["mobile_number": phoneNumberTF.text!, "password" : passwordTF.text!, "uuid" : UIDevice.current.identifierForVendor!.uuidString]
let apiRequest = APIRequest(path: .login, method: .post, body: body, headers: .app)
APIDispatcher.instance.dispatch(request: apiRequest) { result in
self.stopLoading()
switch result {
case .success(let response):
break
case .failure(let error):
break
}
}
}
EDIT: My bad I asked completely reverse on statsCode now I modified it.

Posting data to database with Alamofire failed

I am having an issue with posting data using Alamofire. I am making a comment box. I grab user data from the server and post his comment using his information with his comment on the article id, but when I post it sends no information to the server! I see only empty data.
The user data are successfully loaded from the server and I can see it in the console using the print accountDetails but after posting nothing is shown!
Breakpoint gives valid data too!
My code:
class DetailsViewController: UIViewController {
var data: JSON?
var userData = [JSON]()
var accountDetails = ["name": "", "email": "", "phone": ""]
#IBOutlet weak var CommentTableView: UITableView!
#IBOutlet weak var CommentTXTField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
getUserData()
print("Account:\(accountDetails)")
if let id = Helper.getUserId() {
ContactBtn.isHidden = false
} else {
ContactBtn.isHidden = true
}
}
#IBAction func AddCommentBTN(_ sender: Any) {
let commentTXT = CommentTXTField.text
print(commentTXT!)
let name = self.accountDetails["name"]
let mobile = self.accountDetails["phone"]
let email = self.accountDetails["email"]
let articleId = data!["id"].string!
API.AddComment(articleId: articleId, name: name!, email: email!, phone: mobile!, message: commentTXT!) { (error: Error?, success: Bool) in
if success {
print("Registerd Successfuly")
} else {
print("Faile To Comment")
}
}
}
func getUserData() {
guard let UserId = Helper.getUserMob() else { return }
let url = "https://site.co/apis/getprofile.php?mob=" + UserId
AF.request(url).validate().responseJSON { [self] response in
switch response.result
{
case .failure(let error):
print(error)
case .success(let value):
let json = JSON(value)
if let id = json["data"]["id"].string {
print("id: \(id)")
}
self.accountDetails["name"] = json["data"]["name"].string
self.accountDetails["email"] = json["data"]["email"].string
self.accountDetails["phone"] = json["data"]["phone"].string
}
}
}
}
API.AddComment function
class func AddComment(articleId: String, name: String, email: String, message: String, completion: #escaping (_ error: Error?, _ success: Bool)->Void){
let url = URLs.AddComment
let parameters = [
"article_id": articleId,
"name": name,
"email": email,
"message": message
]
AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default , headers: nil)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result
{
case .failure(let error):
completion(error, false)
print(error)
case .success(let value):
let json = JSON(value)
if let id = json["data"]["id"].string {
print("id: \(id)")
completion(nil, true)
}
}
}
}

POST Json to API with Alamofire?

I want to post a JSON object I create in my service class and pass to the networkService.
This is my network service, but i get an error of
Value of type '[String : Any]' has no member 'data'
on the line: let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
func request(json: [String:Any]) {
let url = URL(string: urlString)!
let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
Alamofire.request(request).responseJSON {
(response) in
print(response)
}
}
The idea being I pass in my JSON when i call the func via the func parameter.
This is the JSON object passed in:
func loginUser(data: Array<String>, deviceToken: String) {
// create JSON
let json = [ "login-email" : data[0],
"login-password" : data[1],
"login-secret" : "8A145C555C43FBA5",
"devicetoken" : deviceToken
]
networkManager.request(json: json)
}
Then I convert and send it to the API (urlString)
Any idea if/why this isnt working?
THanks
Updated revision:
func request(json: [String:Any]) {
let url = URL(string: urlString)!
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options:[])
var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
Alamofire.request(request).responseJSON {
(response) in
print(response)
}
} catch {
print("Failed to serialise and send JSON")
}
}
update: added my code to make a call with completion question:
func sendLoginRequest() {
let userLogin = UserService.init(loginEmail: userEmail, loginPassword: userPassword, loginSecret: loginSecret, deviceToken: deviceToken)
networkService.logUserIn(request: userLogin) { (<#JSON?#>, <#NSError?#>) in
<#code#>
}
}
edit: Updated Payload Shot:
edit 2: mapping issue example:
init?(_ json: JSON) {
// Map API Key from top level
guard let apiKey = json["apikey"].string else { return nil }
// Map User at user level
guard let userDataArray = json["user"].array else {
fatalError("user data array NOT FOUND")
}
print("USER DATA IS \(userDataArray)")
// assign user
for child in userDataArray {
guard let userID = child["id"].int,
let userEmail = child["email"].string,
let lastName = child["lastname"].string,
let firstName = child["firstname"].string,
let company = child["company"].string,
let userImage = child["image"].string,
let jobTitle = child["jobtitle"].string
else { return nil
}
}
// Assign to model properties
self.apiKey = apiKey
self.userEmail = userEmail
self.lastName = lastName
self.firstName = firstName
self.company = company
self.userImage = userImage
self.jobTitle = jobTitle
self.userID = userID
}
I just show how I work with this.
You don't have to convert your parameters to JSON. It's code from Alamofire.
/// A dictionary of parameters to apply to a `URLRequest`.
public typealias Parameters = [String: Any]
Use this method instead of your:
Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: customHeaders)
Try this:
Instead of your request.httpBody = jsonData you can pass your json in parameters.
Your whole code will be:
func request(json: [String:Any]) {
Alamofire.request(urlString, method: .post, parameters: json, encoding: JSONEncoding.default).responseJSON {
(response) in
print(response)
}
}
If you are interested in my approach:
func makePick(request: MakePickRequest, completionHandler: #escaping APICompletionHandler) {
let parameters = request.converToParameters()
Alamofire.request(Endpoints.makePick, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
self.handleResponse(response: response, completionHandler: completionHandler)
}
}
Request:
struct MakePickRequest: GeneralRequest {
let eventId: Int64
let sportId: String
let pickType: PickType
let betType: BetType
let amount: Int
func converToParameters() -> [String : String] {
return ["event_id": String(eventId), "sport_id": sportId,
"pick_type": pickType.rawValue, "bet_type": betType.rawValue,
"amount": String(amount)]
}
}
Structure with endpoints:
struct Endpoints {
// Development baseURL
static let baseURL = "http://myurl/"
private static let apiVersion = "api/v1/"
static var fullPath: String {
return "\(baseURL)\(apiVersion)"
}
// MARK: - User endpoints (POST)
static var login: String {
return "\(fullPath)users/login"
}
static var signUp: String {
return "\(fullPath)users/signup"
}
...
}
Outside of any class (but import SwiftyJSON is obligatory):
typealias APICompletionHandler = (_ data: JSON?, _ error: NSError?) -> Void
Handle response:
private func handleResponse(response: DataResponse<Any>, completionHandler: APICompletionHandler) {
self.printDebugInfo(response)
switch response.result {
case .success(let value):
self.handleJSON(data: value, handler: completionHandler)
case .failure(let error):
print(error)
completionHandler(nil, error as NSError?)
}
}
private func handleJSON(data: Any, handler: APICompletionHandler) {
let json = JSON(data)
let serverResponse = GeneralServerResponse(json)
if (serverResponse?.status == .ok) {
handler(serverResponse?.data, nil)
} else {
handler(nil, self.parseJsonWithErrors(json))
}
}
GeneralServerResponse (depends on your server API):
import SwiftyJSON
final class GeneralServerResponse {
let data: JSON
let status: Status
init?(_ json: JSON) {
guard let status = json["status"].int else {
return nil
}
self.status = Status(status)
self.data = json["data"]
}
enum Status {
case ok
case error
case unauthorized
init(_ input: Int) {
if input >= 200 && input < 400 {
self = .ok
} else if input == 403 {
self = .unauthorized
} else {
self = .error
}
}
}
}
My actual example of usage.
This is outside:
func +=<K, V> ( left: inout [K : V], right: [K : V]) { for (k, v) in right { left[k] = v } }
Example of request:
func makePick(request: MakePickRequest, completionHandler: #escaping APICompletionHandler) {
var parameters = ["auth_token": Preferences.getAuthToken()]
parameters += request.converToParameters()
manager.apiRequest(url: Endpoints.makePick, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
self.handleResponse(response: response, completionHandler: completionHandler)
}
}
SessionManager extension to add headers for all requests:
extension SessionManager {
func apiRequest(url: URLConvertible, method: HTTPMethod, parameters: Parameters? = nil, encoding: ParameterEncoding, headers: HTTPHeaders? = nil) -> DataRequest {
var customHeaders: HTTPHeaders = ["api-key" : "1wFVerFztxzhgt"]
if let headers = headers {
customHeaders += headers
}
return request(url, method: method, parameters: parameters, encoding: encoding, headers: customHeaders)
}
}
In APIManager class:
private let manager: SessionManager
init() {
manager = Alamofire.SessionManager.default
}
Call example:
apiClient.makePick(request: request) { data, error in
if let error = error {
print(error.localizedDescription)
return
}
if let data = data {
// data is a JSON object, here you can parse it and create objects
}
}
Example of class:
import SwiftyJSON
final class MyClass {
let id: Int
let username: String
let parameter: Double
init?(_ json: JSON) {
guard let id = json["id"].int, let username = json["username"].string,
let parameter = json["parameter"].double else {
return nil
}
self.id = id
self.username = username
self.parameter = parameter
}
}

how to get JSON data from login?

I am able to fetch data using Oauth in Alamofire for login successfully but how to get JSON data in Another viewController.
func loginAccess(){
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
"UserName": username_textfield.text! as String,
"Password": password_textfield.text! as String,
"grant_type": "password",
]
// let url = NSURL(string: "http://192.168.100.5:84/Token")!
Alamofire.request("http://192.168.100.5:84/Token", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
let statusCode = (response.response?.statusCode)!
print("...HTTP code: \(statusCode)")
if let JSON = response.result.value {
print("JSON: \(JSON)")
// }
}
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
}
}
break
case .failure(_):
print(response.result.error!)
break
}
}
}
modal class
class Login{
var access_token = String()
var token_type = String()
var expire_in = String()
var username = String()
var masterid = String()
var name = String()
var access = String()
var issued = String()
var expries = String()
init(loginJson:JSON){
self.access_token = loginJson["access_token"].stringValue
self.token_type = loginJson["token_type"].stringValue
self.expire_in = loginJson["expires_in"].stringValue
self.username = loginJson["userName"].stringValue
self.masterid = loginJson["MasterID"].stringValue
self.name = loginJson["Name"].stringValue
self.access = loginJson["Access"].stringValue
self.issued = loginJson[".issued"].stringValue
self.expries = loginJson[".expires"].stringValue
}
}
is this able to access JSON data in Another ViewController or we need another function? if this is possible in same function then how
it could be solved?
Replace this with your code
func getlogin(){
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
"UserName": username_textfield.text! as String,
"Password": password_textfield.text! as String,
"grant_type": "password",
]
// let url = NSURL(string: "http://192.168.100.5:84/Token")!
Alamofire.request("http://192.168.100.5:84/Token", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case.success(let data):
print("success",data)
let statusCode = (response.response?.statusCode)!
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
// self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
}
self.myResponse = JSON(data)
let login = Login(loginJson: self.myResponse)
DispatchQueue.main.async(execute: { () -> Void in
self.performSegue(withIdentifier: "pass_data", sender: login)
})
case.failure(let error):
print("Not Success",error)
}
}
}
you can add this also
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pass_data" {
if let eventsVC = segue.destination as? secondViewController,
let loginData = sender as? Login {
eventsVC.login_details = loginData
}
}
}
in your second view controller you shuold add variable like this var
login_details : Login? to get JSON data
You can pass value using closure .
Create Method
func loginAccess(callBack: (_ responseObject : [Any]) -> Void) -> Void{
if let JSON = response.result.value {
callBack(JSON as Any) // here data passed
}
}
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
callBack([:] as Any)
}
}
Calling method
self.loginAccess { (responseObject) in
print(responseObject) // this is your data
//create your model here
}
How to get data from this login?
func onlinecheckin(){
self.password.resignFirstResponder()
let password1 = password.text;
if let user1 = user.text {
// To trim the characters to remove the empty space
let username = user1.trimmingCharacters(in: CharacterSet.whitespaces)
let passwordnya = password1!.trimmingCharacters(in: CharacterSet.whitespaces)
//Send user data to server side
let myUrl = URL(string: "https://link/jgcm/login.php")!
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"
let postString = "user=\(username) & password=\(passwordnya)"
request.httpBody = postString.data(using: String.Encoding.utf8)
task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
print("response online checking =\(String(describing: response))")
if error != nil {
task.cancel() // To cancel uploading the task.
self.AlertMessage("Error. Please press Login button again.");
print("error=\(String(describing: error))")
return
}
if let datadownload = data {
resultlogin = NSString(data: datadownload, encoding: String.Encoding.utf8.rawValue)!.replacingOccurrences(of: " ", with: "");
print("result of online checking:\(resultlogin)")
}
DispatchQueue.main.async(execute: { () -> Void in
if resultlogin.contains("already") {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "menu")
self.present(secondViewController!,animated: true,completion: nil)
} else {
self.AlertMessage("User atau Password Salah");
}
})
}
task.resume()
}}

Alamofire POST dictionary with row data

Hi everyone I am currently using Swift 2.2 and Alamofire and I am doing a post/form request using a json. Here is the my current code:
func authenticateUserWithValues(passCode : String, userID : String, completion: (result: Bool, user : User?, message : String) -> Void) {
let urlString = NSString(format:"%#%#", kBaseURL, kCheckAuthenticationCodeURL) as String
let parameters: [String: String] = [ "code" : passCode,
"user_id": userID,
"application_type" : "2"]
Alamofire.request(.POST, urlString, parameters: parameters, encoding: .JSON)
.responseJSON { (response) in
switch response.result {
case .Failure(let error):
print(error)
if (error.code == -1009) {
completion(result: false, user: nil, message : kString_No_Internet_Connection)
}else{
completion(result: false, user: nil, message: kString_Unexpected_Error_Occured)
}
case .Success(let responseObject):
let response = responseObject as? [String:String]
var status : String = ""
var message : String = ""
if(response!["status"] != nil){
status = response!["status"]!
}
if(response!["message"] != nil){
message = response!["message"]!
}
if (status == "OK"){
let user : User = RealmManager().addUser(response!)
completion(result: true, user: user, message: message)
}else{
completion(result: false, user: nil, message: message)
}
print(responseObject)
}
}
}
Bu I need to change it to accept a raw body request of the same Dictionary.
Here is the post request in raw format using Swift 2.2:
func authenticateUserWithValues(passCode : String, userID : String, completion: (result: Bool, user : User?, message : String) -> Void) {
let urlString = NSString(format:"%#%#", kBaseURL, kCheckAuthenticationCodeURL) as String
let url:NSURL = NSURL(string: urlString)!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let paramString = "{\"code\" : \"\(passCode)\", \"user_id\" : \"\(userID)\", \"application_type\" : \"2\"}"
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
if (error!.code == -1009) {
completion(result: false, user: nil, message : kString_No_Internet_Connection)
}else{
completion(result: false, user: nil, message: kString_Unexpected_Error_Occured)
}
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
var status : String = ""
var message : String = ""
if(json["status"] != nil){
status = json["status"]! as! String
}
if(json["message"] != nil){
message = json["message"]! as! String
}
if (status == "OK"){
RealmManager().removerAllUsers()
RealmManager().addUser(json)
let user : User = RealmManager().getCurrentUser()!
completion(result: true, user: user, message: message)
}else{
completion(result: false, user: nil, message: message)
}
} catch let error as NSError {
completion(result: false, user: nil, message: error.localizedDescription)
}
}
task.resume()
}

Resources