Created
June 21, 2020 12:05
-
-
Save chaitanyaSoni96/0fd2a4e009e6b021dea7163236b2b0cc to your computer and use it in GitHub Desktop.
Alamofire implementation for generic api calls in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Alamofire | |
import iOSDropDown | |
class ViewController: UIViewController { | |
//Variables | |
@IBOutlet weak var dropDownTxtField: DropDown! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
getFoodCourtData() | |
dropDownTxtField.optionArray = ["Option 1", "Option 2", "Option 3"] | |
dropDownTxtField.didSelect { (text, index, id) in | |
//print("Selected Text is: \(text)") | |
//print("Selected Index is: \(index)") | |
//print("Selected Id is: \(id)") | |
self.dropDownTxtField.text = text | |
print(self.dropDownTxtField.text ?? "") | |
} | |
} | |
} | |
//Drop | |
extension ViewController { | |
} | |
extension ViewController { | |
func getFoodCourtData() { | |
view.makeToastActivity(.center) | |
let url = "http://13.234.40.117/qhospitality/index.php/Webservice/get_food_court_cart_list_by_employee_id" | |
let params = ["co_works_employee_id": 2] | |
WebServiceHelper.instance.getServiceData(url: url, method: .post, parameters: params, encodingType: URLEncoding.default, headers: [:]) { [unowned self] (returnedResponse: FoodCourtData!, errorString: String?) in | |
if errorString != nil { | |
DispatchQueue.main.async { | |
self.view.hideToastActivity() | |
self.view.makeToast(errorString, duration: 2.0, position: .bottom) | |
} | |
} | |
else { | |
if let returnedResponse = returnedResponse { | |
if returnedResponse.code == 0 { | |
//Success | |
self.view.hideToastActivity() | |
print(returnedResponse.results) | |
} | |
else { | |
//Failure | |
DispatchQueue.main.async { | |
self.view.hideToastActivity() | |
self.view.makeToast(returnedResponse.message, duration: 2.0, position: .bottom) | |
} | |
} | |
} | |
} | |
} | |
} | |
// WebServiceHelper.instance.getServiceData(url: url, method: .post, parameters: params, encodingType: URLEncoding.default, headers: [:]) { [unowned self] (foodCourtResponse: FoodCourtData),<#arg#> in | |
// | |
// if foodCourtResponse.code == 0 { | |
// print(foodCourtResponse.results) | |
// DispatchQueue.main.async { | |
// self.view.hideToastActivity() | |
// } | |
// } | |
// else { | |
// print(foodCourtResponse.message) | |
// DispatchQueue.main.async { | |
// self.view.hideToastActivity() | |
// self.view.makeToast(foodCourtResponse.message, duration: 2.0, position: .bottom) | |
// } | |
// } | |
// | |
// } | |
// | |
// } | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Alamofire | |
class WebServiceHelper { | |
static let instance = WebServiceHelper() | |
let connectivityManager = NetworkReachabilityManager() | |
let manager = Alamofire.SessionManager() | |
func getServiceData <T: Codable> (url: String, method: HTTPMethod, parameters: [String:Any], encodingType: ParameterEncoding, headers: [String:String], completion: @escaping (T?, String?) ->()) { | |
if connectivityManager?.isReachable ?? false { | |
//print("Network is Reachable") | |
let manager = Alamofire.SessionManager.default | |
manager.session.configuration.timeoutIntervalForRequest = 20 | |
manager.request(url, method: method, parameters: parameters, encoding: encodingType, headers: headers).responseJSON { (response) in | |
if response.error != nil { | |
//debugPrint(response.error?.localizedDescription ?? "") | |
completion(nil, response.error?.localizedDescription) | |
} | |
else { | |
guard let data = response.data else {return completion(nil, response.error?.localizedDescription)} | |
let decoder = JSONDecoder() | |
do{ | |
let returnedResponse = try decoder.decode(T.self, from: data) | |
completion(returnedResponse, nil) | |
}catch{ | |
//debugPrint(error) | |
completion(nil, error.localizedDescription) | |
} | |
} | |
} | |
} | |
else { | |
//print("Network Not Reachable") | |
completion(nil, "Network Not Reachable") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment