passing data (json) to another controller in swift 5.2 con Alamofire - ios

I have a Login view that asks for a card and password. I consult an API and if the entered data is correct, it sends me a JSON like this. Which return has the button method? How do I send that data to the other view? I occupy Alamofire 5.0 and have my Model class.
#IBAction func myButtonIngresarAction(_ sender: Any) {
guard let carnet = self.txtCarnet.text else {return}
guard let contrasena = self.txtPassword.text else {return}
let ingresologinmodel = IngresoLoginModel(usuario: carnet, password: contrasena)
self.apiCall(IngresoLoginModel: ingresologinmodel){
(result) in
switch result{
case .success(let json):
print(json)
**//This is where I want to send that json with the data to the other view. ******
case .failure(let err):
print(err.localizedDescription)
}
}
}
enum ApiErros: Error {
case custom(message : String)
}
typealias Handler = (Swift.Result<Any?, ApiErros>) -> Void
func apiCall(IngresoLoginModel: IngresoLoginModel, completionHandler: #escaping Handler)
{
let header: HTTPHeaders = [
.contentType("application/json")
]
AF.request("https://url/xxxx/api/Login", method: .post, parameters: IngresoLoginModel,
encoder: JSONParameterEncoder.default, headers: header).response{ response in
debugPrint(response)
switch response.result{
case .success(let data):
do{
let json = try JSONDecoder().decode([LoginModel].self, from: data!)
print(json)
if response.response?.statusCode == 200{
completionHandler(.success(json))
}else{
completionHandler(.failure(.custom(message: "Por favor verifica tu internet")))
}
}
catch
{
print(error)
completionHandler(.failure(.custom(message: "Problemas")))
}
case .failure(let err):
print(err.localizedDescription)
}
}
}
Class model
struct LoginModel: Codable {
let idEmpleado: Int
let Nombre: String
let CodEmpleado: String
let password: String
let idPerfil: Int
let activo: Int
let Descripcion: String
let idRegion: Int
let correo: String
}
This is the json that the Api sends me the data changes them for these example
{
"idEmpleado": 1,
"nombre": “test”,
"codEmpleado": “000000”,
"password": “123”,
"idPerfil": 4,
"activo": 1,
"Descripcion": “test”,
"idregion": 1,
"correo": “test#test.com"
}

many way like create a variable to save this json in OtherViewController and call, self?.otherViewController.json = json
https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

use didSet
var page = [Datas]() {
didSet {
self.myVariable = page[0].date!
}
}

typealias Handler = (Swift.Result <[LoginModel]?, ApiErros>) -> Void
#IBAction func myButtonIngresarAction(_ sender: Any) {
guard let carnet = self.txtCarnet.text else {return}
guard let contrasena = self.txtPassword.text else {return}
let ingresologinmodel = IngresoLoginModel(usuario: carnet, password: contrasena)
self.apiCall(IngresoLoginModel: ingresologinmodel){
(result) in
switch result{
case .success(let json):
print(json)
//Here is that I do not know how to send it to the other controller all the json
let viewControllerB = HomeMenuViewController()
viewControllerB.datosPersonales = json!
self.navigationController?.pushViewController(viewControllerB, animated: true)
case .failure(let err):
print(err.localizedDescription)
}
}
}
second controller
class HomeMenuViewController: UIViewController {
#IBOutlet weak var mylabel: UILabel!
var datosPersonales = [LoginModel]()
override func viewDidLoad() {
super.viewDidLoad()
print("***************")
print(datosPersonales)
print("***************")
}
}

Related

JSON Decoding Not Populating Table View

I am trying to parse data from the website movieDatabase.com However there's some issue decoding the data to json and populating my table view.I am not sure why this is happening. Please I need help spotting out the problem. Here's my code. https://github.com/lexypaul13/Movie-Browser/tree/main/Movie-Browser
struct Movies: Codable {
let overview:String?
let original_title: String?
let poster_path:String
}
struct ApiResponse:Codable, Hashable {
let page:Int
let shows:[Movies]
enum CodingKeys:String, CodingKey {
case page = "page"
case shows = "results"
}
}
class NetworkManger{
enum EndPoint{
case showList
}
static let shared = NetworkManger()
private let baseURL : String
private var apiKeyPathCompononent :String
private init(){
self.baseURL = "https://api.themoviedb.org/3/movie/now_playing?"
self.apiKeyPathCompononent = "api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed"
}
private var jsonDecoder:JSONDecoder = {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()
func get<T:Decodable>(_ endPoints: EndPoint, urlString: String, completed:#escaping(Result<T?,ErroMessage>)->Void){
guard let url = urlBuilder(endPoint: endPoints) else {
completed(.failure(.invalidURL))
return
}
let task = URLSession.shared.dataTask(with: url){ data, response, error in
if let _ = error {
completed(.failure(.unableToComplete))
return
}
guard let response = response as? HTTPURLResponse, response.statusCode==200 else {
print(ErroMessage.invalidResponse.rawValue)
completed(.failure(.invalidResponse))
return
}
guard let data = data else{
completed(.failure(.invalidData))
return
}
do{
let apiResponse = try self.jsonDecoder.decode([T].self, from: data)
DispatchQueue.main.async {
completed(.success(apiResponse as? T))
}
} catch{
print(ErroMessage.invalidData.rawValue)
}
}
task.resume()
}
private func urlBuilder(endPoint:EndPoint )->URL?{
switch endPoint {
case .showList:
return URL(string: baseURL + apiKeyPathCompononent )
}
}
func getMovies(){
NetworkManger.shared.get(.showList, urlString: "") { [weak self] (result: Result<[Movies]?,ErroMessage> ) in
guard let self = self else { return }
switch result{
case .success(let movies):
self.movies = movies ?? []
DispatchQueue.main.async {self.tableView.reloadData()}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
The root object returned from the api is your ApiResult struct. This contains an array of movies (which you have mapped to the shows property of the ApiResult)
You need to change the getMovies function so that the right generic type can be inferred and the json decoder can do the right thing
func getMovies(){
NetworkManger.shared.get(.showList, urlString: "") { [weak self] (result: Result<ApiResult,ErroMessage> ) in
guard let self = self else { return }
switch result{
case .success(let apiResult):
self.movies = apiResult.shows
DispatchQueue.main.async {self.tableView.reloadData()}
case .failure(let error):
print(error.localizedDescription)
}
}
}

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)
}
}
}
}

How to reflect data from the Get Request of Alamofire going to the UI View Controller

How can I reflect the data from the JSON Dictionary using Alamofire to perform validation to check whether the passcode entered is valid or not to my UI View Controller. I already got a Successful result in getting data from the API but I don't know how can my view controller read the values from the API to validate the passcode entered. Please help me.......
APIService
class APIService
{
let eventAPIKey: String
let eventBaseURL: URL?
//static let kEventID = "id"
init(APIKey: String)
{
self.eventAPIKey = APIKey
eventBaseURL = URL(string: BASE_URL)
}
func validatePasscode(passcode: String, completion: #escaping (Event?) -> Void)
{
if let passcodeURL = URL (string: "\(PASSCODE_CHECKER_URL)/\(passcode)") {
Alamofire.request(passcodeURL, method: .get).responseJSON { (response) in
switch response.result{
case .success:
if let passcodeJSON = response.result.value {
print(passcodeJSON)
completion(passcodeJSON as? Event)
}
case .failure(let error):
print("\(error)")
}
}
}
}
}
ViewController
func validateEventPasscode(){
let api = APIService(APIKey: passcode)
api.validatePasscode(passcode: passcode) { (event) in
if let eventDetails = self.event {
self.event = eventDetails
self.view.squareLoading.stop(0.0)
self.performSegue(withIdentifier: "showEventDashboard", sender: self)
self.enteredEventCode.text = ""
}

ios - Cannot get data from Alamofire return

I want to get the data from server api using Alamofire call. But after the api function executed, the data return is empty because Swift is asynchronous...
This code is call the server api:
func getAllModels(completion: #escaping (_ result: [String]?) -> ()) {
var _modelList:[String] = []
let url = BASE_URL + "getAllProductAndModelv2"
Alamofire.request(url, method:.get, parameters: [:], encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success:
if((response.result.value) != nil) {
let data = NSData(contentsOf: URL(string: url)!)
do {
if let data = data, let json = try JSONSerialization.jsonObject(with: data as Data) as? [String: Any], let models = json["models"] as? [[String:Any]] {
for model in models {
if let name = model["name"] as? String {
_modelList.append(name)
}
}
}
}catch {
print("error")
}
completion(_modelList)
}
case .failure(let error):
print(error)
completion(nil)
}
}
}
And this code is get data from getAllModels function:
var models:[VirtualObject] = []
RestApiManager().getAllModels(){ (result) -> () in
print("************************************************")
if let result = result {
var array = result as Array
for item in array {
print(item)
models.append(VirtualObject(url: URL(string: item)!)!)
}
}
print("************************************************")
}
print(models)
return models
I don't know how to use the callback function exactly to bind the data to return model.. Please help!
Use didSet observer of variables. And call api in viewDidload.
class ViewController: UIViewController {
var arrModals = [Any]() {
didSet {
print("this call when get all modals from server")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
RestApiManager().getAllModels(){ (result) -> () in
var arrTempModals = [Any]()
if let result = result {
var array = result as Array
for item in array {
print(item)
arrTempModals.append(item)
}
}
self.arrModals = arrTempModals
}
}
}

Async task don't change external variable. Swift 3

I can't save data from URL, because function in infinit loop. How fix it?
My code:
func getRegion2(){
let method = "region/"
var url = serviceUrl+method
var myArray: [String]()
while(url != nil){
Alamofire.request(url).validate().responseJSON { response in
switch response.result {
case .success(let data):
let nextUrl = JSON(data)["next"].stringValue
url = nextUrl
myArray = myArray + myArray
print(nextUrl)
case .failure(let error):
print("Request failed with error: \(error)")
}
}
}
print(myArray)
}
If run without the "while", then everything works fine.
One possible solution is to combine a recursive function and a dispatch group (not tested):
func getRegion2(){
let method = "region/"
var url = serviceUrl+method
var myArray: [String] = []
let group = DispatchGroup()
func getRegion(with url: String) {
group.enter()
Alamofire.request(url).validate().responseJSON { response in
switch response.result {
case .success(let data):
let nextUrl = JSON(data)["next"].stringValue
myArray = myArray + someArrayFromRespnse
print(nextUrl)
if nextUrl != nil {
getRegion(with: nextUrl)
}
group.leave()
case .failure(let error):
print("Request failed with error: \(error)")
}
}
}
getRegion(with: url)
group.notify(queue: DispatchQueue.main) {
print(myArray)
}
}
I would use a completionBlock:
func getRegion2(completion: () -> [String]?) {
let method = "region/"
var url = serviceUrl+method
var myArray: [String] = []
func getRegion(with url: String) {
Alamofire.request(url).validate().responseJSON { response in
switch response.result {
case .success(let data):
let nextUrl = JSON(data)["next"].stringValue
myArray = myArray + someArrayFromRespnse
print(nextUrl)
if nextUrl != nil {
getRegion(with: nextUrl)
} else {
completion(myArray)
}
case .failure(let error):
completion(nil)
}
}
}
getRegion(with: url)
}

Resources