Related
I am creating an app which requires to pull data from API, the scenario is, I will get the json data below:
[
{
"chargeId": "33fbbbd0-2e33-11e9-a2cb-8a27ecbbcb73",
"chargeDate": "2019-02-12T03:28:44.000",
"vatRate": "NON-VAT",
"taxRate": 0.1,
"policyGroup": "Patient Participation",
"itemDescription": "Ecg at er/icu df",
"scdFlag": 0,
"amounts": null,
"scdDiscounts": 0,
"othDiscounts": 4.54,
"adjustment": 0,
"pfBill": 222.46,
"vatAmount": 0,
"taxableAmount": 11.12,
"merchantDiscount": 0,
"creditedAmount": 211.3,
"chargeAmount": null,
"previousCredits": null
},
{
"chargeId": "5a2cabc1-46c9-11e9-a2cf-863c7cdffd18",
"chargeDate": "2019-03-15T10:24:21.000",
"vatRate": "NON-VAT",
"taxRate": 0.1,
"policyGroup": "Patient Participation",
"itemDescription": "Professional Fees",
"scdFlag": 0,
"amounts": null,
"scdDiscounts": 0,
"othDiscounts": 0,
"adjustment": 0,
"pfBill": 1000,
"vatAmount": 0,
"taxableAmount": 100,
"merchantDiscount": 0,
"creditedAmount": 900,
"chargeAmount": null,
"previousCredits": null
}
]
I did pulled the data successfully by using the Alamofire code below:
typealias getPatientDetailsPerPayoutTaskCompletion = (_ patientDetailsPerPayout: [PatientPayoutDetails]?, _ error: NetworkError?) -> Void
static func getPatientDetailsPerPayout(periodId: Int, doctorNumber: String, parameterName: PatientParameter, hospitalNumber: String, completion: #escaping getPatientDetailsPerPayoutTaskCompletion) {
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
completion(nil, .invalidURL)
return
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getAllTasks { (tasks) in
tasks.forEach({ $0.cancel() })
}
Alamofire.request(patientDetailsPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
print(patientDetailsPerPayoutURL)
guard HelperMethods.reachability(responseResult: response.result) else {
completion(nil, .noNetwork)
return
}
guard let statusCode = response.response?.statusCode else {
completion(nil, .noStatusCode)
return
}
switch(statusCode) {
case 200:
guard let jsonData = response.data else {
completion(nil, .invalidJSON)
return
}
let decoder = JSONDecoder()
do {
let patientDetailsPayout = try decoder.decode([PatientPayoutDetails].self, from: jsonData)
completion(patientDetailsPayout, nil)
} catch {
completion(nil, .invalidJSON)
}
case 400: completion(nil, .badRequest)
case 404: completion(nil, .noRecordFound)
default:
print("**UNCAPTURED STATUS CODE FROM (getPatientDetailsPayout)\nSTATUS CODE: \(statusCode)")
completion(nil, .uncapturedStatusCode)
}
}
The JSON Data will display in a CollectionCell, and user will tapped the cell to view the data under one chargedId but unfortunately, when I tapped the cell the all data are pulled instead of one part of the array only. The code below is what I used to pull just part of the array:
typealias getSelectedPatientItemDetailsTaskCompletion = (_ selectedpatient: PatientPaymentDetails?, _ error: NetworkError?) -> Void
static func getPatientItemDetails(periodId: Int, doctorNumber: String, parameterName: PatientParameter, hospitalNumber: String, chargeId: String, completion: #escaping getSelectedPatientItemDetailsTaskCompletion) {
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
completion(nil, .invalidURL)
return
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getAllTasks { (tasks) in
tasks.forEach({ $0.cancel() })
}
Alamofire.request(patientDetailsPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
print(patientDetailsPerPayoutURL)
guard HelperMethods.reachability(responseResult: response.result) else {
completion(nil, .noNetwork)
return
}
guard let statusCode = response.response?.statusCode else {
completion(nil, .noStatusCode)
return
}
switch(statusCode) {
case 200:
guard let jsonData = response.data else {
completion(nil, .invalidJSON)
return
}
let decoder = JSONDecoder()
do {
let patientDetailsPayout = try decoder.decode(PatientPaymentDetails.self, from: jsonData)
completion(patientDetailsPayout, nil)
} catch {
completion(nil, .invalidJSON)
}
case 400: completion(nil, .badRequest)
case 404: completion(nil, .noRecordFound)
default:
print("**UNCAPTURED STATUS CODE FROM (getPatientDetailsPayout)\nSTATUS CODE: \(statusCode)")
completion(nil, .uncapturedStatusCode)
}
}
}
}
}
Didselect Function to pull data
switch indexPath.section {
case 0:
self.selectedCardIndex = indexPath
let selectedItem = selectedItemDescription.id
getItemDetails(parameter: .searchByChargedId, from: selectedItem)
let cardController = UserCardViewController.init(nibName: "UserCardViewController", bundle: nil)
present(cardController, animated: true, completion: nil)
default: break
}
}
getItemDetails Function
func getItemDetails(parameter: PatientParameter, from: String) {
APIService.PatientList.getPatientItemDetails(periodId: currentRemittance.periodId, doctorNumber: doctorNumber, parameterName: parameter, hospitalNumber: patient.hospitalNumber!, chargeId: from) { (getItem, error) in
guard let pageItemDescription = getItem, error == nil else {
SVProgressHUD.dismiss()
return
}
switch parameter {
case .selectedByChargedID:
if self.patientPayoutDetails == nil {
self.selectedPatientItemDescription = pageItemDescription
}else {
self.patientPayoutDetails.append(contentsOf: pageItemDescription.chargedId)
}
default: break
}
SVProgressHUD.dismiss()
}
}
Hope you can help me, sorry if I included almost all the code but I just want to show you the flow of my codes. Been working on it for almost 1 week. Thank you.
Both of your functions getPatientItemDetails and getPatientDetailsPerPayout are retrieving data from the same URL:
guard let patientDetailsPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPayoutDetails)?periodId=\(periodId)&doctorNumber=\(doctorNumber)\(parameterName.rawValue)\(hospitalNumber)") else {
You probably have different URL for different endpoints; verify the URL for both methods making sure you use the appropriate ones.
I am new to Unit Testing. I have tested a lot of functions and I understood the concept, now I want to check the APIs. Is it possible? I guess so. This is the API:
func sendRequest(path: String, params: Dictionary<String, Any>, showSpinner: Bool, completionHandler: #escaping (JSON, Error?) -> Void) {
if Constants.IS_SIMULATOR {
print("Path: \(path)")
print("Params: \(params)")
}
if Constants.APP_DEL.reachability?.connection == .none {
completionHandler(JSON.null, NSError(domain: "No internet", code: 4, userInfo: nil))
return
}
UIApplication.shared.isNetworkActivityIndicatorVisible = true
if showSpinner {
HUD.show(.labeledProgress(title: "Loading...", subtitle: "Please wait"))
}
if let jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) {
let url = NSURL(string: String(format: "%#%#", Constants.TEST_URL, path))!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = jsonData
request.timeoutInterval = 120
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
DispatchQueue.main.async {
if error != nil {
print(" ........ \(String(describing: error?.localizedDescription))")
UIApplication.shared.isNetworkActivityIndicatorVisible = false
if showSpinner {
HUD.flash(.labeledError(title: "Server issue", subtitle: "Invalid response"), delay: 2.0)
}
completionHandler(JSON.null, NSError(domain: "Invalid response", code: 420, userInfo: nil))
return
}
if (data?.isGzipped)! {
let decompressedData: Data = try! data!.gunzipped()
var json: JSON = JSON.null
do {
json = try JSON(data: decompressedData)
}
catch {
print(error)
}
if Constants.IS_SIMULATOR {
print("Response: \(json)")
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
if json["status"].int == 200 {
if showSpinner {
HUD.flash(.success, delay: 0.5)
}
completionHandler(json["data"], nil)
}
else if json["status"].int == 202 {
if showSpinner {
HUD.hide()
}
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
else if json["status"].int == 310 {
if showSpinner {
HUD.hide()
}
completionHandler(json["data"], nil)
}
else if json["status"].int == 403 {
if showSpinner {
HUD.hide()
}
GeneralHelper.sharedInstance.displayAlertMessage(titleStr: "Session expired", messageStr: "Kindly login again.")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.synchronize()
Constants.APP_DEL.navC?.popToRootViewController(animated: false)
})
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
else {
if showSpinner {
HUD.flash(.labeledError(title: "", subtitle: json["message"].string!), delay: 2.0)
}
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
}
else {
let backToString = String(data: data!, encoding: String.Encoding.utf8) as String?
if Constants.IS_SIMULATOR {
print("Invalid response: \(String(describing: backToString))")
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
if showSpinner {
HUD.flash(.labeledError(title: "Server issue", subtitle: "Invalid response"), delay: 2.0)
}
completionHandler(JSON.null, NSError(domain: "Invalid response", code: 420, userInfo: nil))
}
}
}
task.resume()
}
}
So, to test this I have done this:
func testAPIWorking() {
params = ["ios_token": "dq6YJkKwEx0:APA91bFeOTfJRFd5G78xMkv3AvjSLA7ey2dJxTZZAtMuuC50CqWILNzNjdgqVpNpDn7R4I0DLoydIVDYKubpGfgfu1bwz1H3VNU4D88ek8PJTAjxrd3CWkW78g0sNv6EZDLlTqUFeNxh", "api_token": "kfRSHL0bVP1fSmxNY3NfEGs8g0ktKCbTsPRRbfarh3a5ISIcZLu3qdK07MJ9H4rJ", "player_id": 8083]
ServiceHelper.sharedInstance.sendRequest(path: "home", params: self.params, showSpinner: false) { (result, error) in
if error != nil {
XCTFail("Fail")
}
else {
}
}
}
I have added a breakpoint at task and it prints the task but then when I try to move to next line, instead of going into Dispatch it takes me out of that and stops at task.resume() and because of this I am not able to test the errors or expected results. Any help?
Here you have a completion handler, the api call in not sync. So you should to wait the result in your test. In Xcode you could use XCTestExpectation.
For example:
func testAPIWorking()
{
let expectation = XCTestExpectation.init(description: "Your expectation")
params = ["ios_token": "dq6YJkKwEx0:APA91bFeOTfJRFd5G78xMkv3AvjSLA7ey2dJxTZZAtMuuC50CqWILNzNjdgqVpNpDn7R4I0DLoydIVDYKubpGfgfu1bwz1H3VNU4D88ek8PJTAjxrd3CWkW78g0sNv6EZDLlTqUFeNxh", "api_token": "kfRSHL0bVP1fSmxNY3NfEGs8g0ktKCbTsPRRbfarh3a5ISIcZLu3qdK07MJ9H4rJ", "player_id": 8083]
ServiceHelper.sharedInstance.sendRequest(path: "home", params: self.params, showSpinner: false) { (result, error) in
if error != nil
{
XCTFail("Fail")
}
// The request is finished, so our expectation
expectation.fulfill()
}
// We ask the unit test to wait our expectation to finish.
self.waitForExpectations(timeout: 20)
}
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 }
}
I have recorded audio with iOS successfully, but the problem is that I cannot send audio to server. Code that I tried is as shown below.
let audioData : NSData = try Data(contentsOf: (audioRecorder?.url)!) as NSData
var finalurl = url+Access_Token!
let params = [
"name":"iosTest.mp3",
"file": audioData] as [String : Any]
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30000
manager.request(finalurl, method: .post, parameters: params).responseJSON {
response in
stopActivityIndicator()
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
let ResponseSuccess = JSON.object(forKey: "response")!
displayAlertMessage(userMessage: ResponseSuccess as! String, myView: self)
}
}
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
displayAlertMessage(userMessage: "Something error Occured! Please try again" as! String, myView: self)
}
Have you tried this from their official documentation on GitHub? Uploading Multipart Form Data
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(audioRecorder?.url, withName: "iosTest.mp3")
},
to: "https://yourLinkGoesHere",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
or this Uploading a File
Alamofire.upload(audioRecorder?.url, to: "https://yourLinkGoesHere").responseJSON { response in
debugPrint(response)
}
SWIFT 5, Alamofire 5.
File with the request and completion #escaping :
import Alamofire
import SwiftyJSON
func request(variable1: String?, variable2: String?, audioFilePath: URL, completion: #escaping (String) -> ()) {
let url = URL(string: "https://...")!
let headers: HTTPHeaders = [
"content-type": "multipart/form-data; boundary=---011000010111000001101001",
"accept": "application/json",
"authorization": "Bearer \(your_token_if_needed)"
]
var parameters: [String: Any] = [:]
if variable1 != nil { parameters["var1"] = variable1 }
if variable2 != nil { parameters["var2"] = variable2 }
// Let's check the data
print(parameters)
AF.upload(multipartFormData: { multipartFormData in
for (key,value) in parameters {
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
multipartFormData.append(audioFilePath, withName: "audio.m4a")
}, to: url, headers: headers)
.responseJSON { response in
switch response.result {
case .success:
do{
let json = try JSON(data: response.data!)
print(json)
// Parse an Array
let statusJson = json["status"].string
if statusJson == "success" {
completion("success")
}
else
{ completion(jsonErrorMsg!) }
} catch {
print(error.localizedDescription)
}
case .failure(let encodingError):
print("error:\(encodingError)")
}
}
}
Your ViewController class (Where you want to record the audio file)
// 1 - Delegation
class ViewController: UIViewController, AVAudioRecorderDelegate {
// 2 - Variables
var audioRecorder: AVAudioRecorder!
var audioFilename: URL!
var meterTimer:Timer!
var isAudioRecordingGranted: Bool!
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - permission
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSessionRecordPermission.granted:
isAudioRecordingGranted = true
audioRecorderAction()
break
case AVAudioSessionRecordPermission.denied:
isAudioRecordingGranted = false
break
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
self.isAudioRecordingGranted = true
self.audioRecorderAction()
} else {
self.isAudioRecordingGranted = false
}
}
}
break
default:
break
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
audioRecorder = nil
}
func audioRecorderAction(){
if isAudioRecordingGranted {
//Create the session.
let session = AVAudioSession.sharedInstance()
do {
//Configure the session for recording and playback.
try session.setCategory(.playAndRecord, mode: .default, options: [])
try session.setActive(true)
//Set up a session
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.medium.rawValue
]
//file name URL
audioFilename = getDocumentsDirectory().appendingPathComponent("audio.m4a")
//Create the audio recording, and assign ourselves as the delegate
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.record()
meterTimer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector:#selector(self.updateAudioTimer(timer:)), userInfo: nil, repeats: true)
}
catch let error {
print("Error for audio record: \(error.localizedDescription)")
}
}
}
func finishAudioRecording(success: Bool) {
audioRecorder.stop()
audioRecorder = nil
meterTimer.invalidate()
if success {
print("Finished.")
} else {
print("Failed :(")
}
}
#objc func updateAudioTimer(timer: Timer) {
if audioRecorder.isRecording {
let min = Int(audioRecorder.currentTime / 60)
let sec = Int(audioRecorder.currentTime.truncatingRemainder(dividingBy: 60))
let totalTimeString = String(format: "%02d:%02d", min, sec)
recordingTimeLabel.text = totalTimeString
audioRecorder.updateMeters()
if (sec == 10) {
finishAudioRecording(success: true)
}
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
//MARK:- Audio recoder delegate methods
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if !flag {
finishAudioRecording(success: false)
request(variable1: var_to_send1, variable2: var_to_send2, audioFilePath: audioFilename, completion: { (response) -> Void in
if response == "success" {
print("success")
} else {
print("error")
}
})
}
}
I would like to post a JSON via Alamofire.
but i'm not too sure how can i deal with it.
my swiftyJSON is in an array as my
how can i encode an array of JSON into a dictionaryObject? to suits
Alamofire's Parameters?
urlRequest = try JSONEncoding.default.encode(urlRequest, with: location)
my sample JSON looks like this:
"[{\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}]"
The trick is you'll need to convert it to Dictionary.
Sample snippet as follow:
```
// let assume swiftyJSON is a SwiftyJSON object (JSON)
if let data = swiftyJSON.rawString()!.data(using: .utf8) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
Alamofire.request(url, method: .post, parameters: json, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
debugPrint(response)
}
} catch {
print("JSONSerialization error")
}
}
```
First add SwiftJSON to your project then
class func requestPOSTURL(serviceName:String,parameters: [String:Any]?, completionHandler: #escaping (JSON?, NSError?) -> ()) {
let headersSet: HTTPHeaders = [
"Authorization":GlobalAccesstoken,
"Accept": "application/json"
]
Alamofire.request(serviceName, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headersSet).responseJSON {
(response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if let data = response.result.value{
let json = JSON(data)
completionHandler(json,nil)
}
break
case .failure(_):
completionHandler(nil,response.result.error as NSError?)
break
}
}
}
AFWrapper.requestPOSTURL(serviceName: LapiUrl+"get_profile", parameters: params) { (response:JSON?, error:NSError?) in
if error != nil {
print(error!)
return
}
if response == nil {
return
}
print(response!)
var distRespoce = response!.dictionary?["response"]?.array?[0]
if (distRespoce?["status"].string == "true"){
let distuserData = distRespoce!.dictionary?["user_data"]
}
else{
print("no")
}
}
Try above code ..
You can do like this : -
Alamofire.request(urlString, method: .post, parameters: paramData, encoding:JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result)
{
case .success(_):
if response.result.value != nil
{
do
{
var dict : NSDictionary = try JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
print(dict)
dict = UtilityClass.removeNullFromResponse(response: response.result.value! as! NSDictionary)
self.dataBlock(dict,nil)
}
catch
{
UtilityClass.hideHudLoading()
}
}
break
case .failure(_):
if response.result.error != nil
{
print(response.result.error!)
UtilityClass.hideHudLoading()
}
break
}
}
}
Simple Get post code, Easy to use, maintain and understand
Below is pod, you must need to use my code of json parsing.
#Network manager related
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => ‘4.0.1’
pod 'AlamofireNetworkActivityIndicator', '~> 2.0'
pod 'AlamofireObjectMapper', '~> 4.0.0'
pod 'SVProgressHUD', :git => 'https://github.com/SVProgressHUD/SVProgressHUD.git'
pod 'Reachability', '~> 3.2'
pod 'SwiftyJSON', '~> 3.0.0'
pod 'ObjectMapper', '~> 2.0'
pod 'SDWebImage', '~> 3.8'
Here, You can Call API in your view controller.
RegistrationService().login(email: (txtEmail.text)!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines), password: self.txtPassword.text!, success: { (response) in
//Save data to user default
}) { (error) in
print(error as Any)
}
RegistrationService.swift //Service class, in which you can add api's
//
// RegistrationService.swift
// hotelBids
//
// Created by Mehul Parmar on 27/05/16.
// Copyright © 2017 Sooryen Innovation labs. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
import ObjectMapper
import AlamofireObjectMapper
open class RegistrationService {
enum listOfApi : String {
case
login,
country
}
func country(_ success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
// create post request
NetworkManager().Get(listOfApi.country.rawValue,
paramaters: [:],
showLoader: true,
success: { responseObject in
success(responseObject)
}) { error in
failure(error)
}
}
func login(email: String, password: String, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
// create request parameter
let requestParameters = ["email" : email,
"password" : password,
"user_role" : "customer"
]
// create post request
NetworkManager().POST(listOfApi.login.rawValue,
paramaters: requestParameters as [String : AnyObject]?,
showLoader: true,
success: { responseObject in
success(responseObject)
}) { error in
failure(error)
}
}
NetworkManager.swift
//
// NetworkManager.swift
// Constants.kAppName.localized()
//
// Created by Mehul Parmar on 08/06/16.
// Copyright © 2016 . All rights reserved.
//
import Foundation
import Alamofire
import SVProgressHUD
import SwiftyJSON
//import AMSmoothAlert
import ObjectMapper
import AlamofireObjectMapper
//used for facebook logour while invalid session
import FacebookLogin
//MARK : - Errors
enum NetworkConnection {
case available
case notAvailable
}
class NetworkManager {
let baseUrlDev_OLD : String = "https://hotelbids.com/" + "dev/" + "hb-app/" + "v3/" + "user/"
let baseUrlDev : String = "http://184.73.131.211/api/v1/"
//MARK : - Shared Manager
let sharedManager = SessionManager()
func getHeaders(_ urlString: String) -> [String: String] {
var headerDictionary = [String: String]()
if UserDetail.rememberToken != nil {
if (UserDetail.rememberToken?.length)! > 0 {
headerDictionary[Constants.KEY_remember_token] = "\(UserDetail.rememberToken!)"
}
}
/*
if let xapi = UserDefault.getXapi() {
headerDictionary[Constants.KEY_Xapi] = xapi
}
if let accessLanguage = UserDefault.getLanguage() {
headerDictionary[Constants.KEY_Language] = accessLanguage
}
if let userId = UserDefault.getUserId() {
headerDictionary[Constants.KEY_USER_ID] = userId
}
if let accessToken = UserDefault.getAccessToken() {
headerDictionary[Constants.KEY_AccessToken] = accessToken
}*/
print("urlString: \(urlString)\nheaderDictionary : \(headerDictionary)")
return headerDictionary
}
func printResponse(urlString: String, paramaters: [String: AnyObject]?, response: AnyObject) {
let dictResponce = self.getValidatedData(response as AnyObject)
// if dictResponce.boolValue {
if let paramatersTemp = paramaters {
if paramatersTemp.values.count > 0 {
let jsonParameters = JSON(paramatersTemp)
print("\n\n\nurlString : \(urlString) ,\n\n paramaters: \(jsonParameters) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
else {
print("\n\n\nurlString : \(urlString) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
}
else {
print("\n\n\nurlString : \(urlString) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
// } else {
// print("urlString : \(urlString) ,\n Response: \(String(describing: dictResponce))")
// }
}
func getValidatedData(_ response: AnyObject?) -> JSON {
//Removing null and <null>, and replacing number or integer to string
guard var dictResponse = response as? NSDictionary else{
return nil
}
dictResponse = (dictResponse.replacingNullsWithStrings() as NSDictionary).convertingNumbersToStrings()! as NSDictionary
let jsonResponce = JSON(dictResponse)
return jsonResponce
}
// MARK: - Get Method
func Get(_ urlString: String, paramaters: [String: AnyObject]? = nil, showLoader: Bool? = nil, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
switch checkInternetConnection() {
case .available:
if let showLoader = showLoader {
if showLoader {
DispatchQueue.main.async {
// update some UI
UIApplication.shared.keyWindow?.showLoader()
}
}
}
Alamofire.request(baseUrlDev.add(urlString: urlString), method: .get, parameters: paramaters, encoding: URLEncoding.default, headers: self.getHeaders(urlString)).responseJSON (completionHandler: { response in
DispatchQueue.main.async {
if UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.endIgnoringInteractionEvents()
}
if let showLoader = showLoader {
if showLoader {
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
}
}
}
//Success
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
//Print response using below method
self.printResponse(urlString: urlString, paramaters: paramaters, response: (value as AnyObject))
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
else {
//Check response error using status code
if let strErrorReasonCode : Int = response.response?.statusCode {
if let data = response.data {
let jsonResponce = JSON(data: data)
if strErrorReasonCode == 500 {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
return
}
if let dictionary : NSDictionary = jsonResponce.dictionaryObject as NSDictionary? {
let responce : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictionary)
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let Alamofire_Error = -1005
if authentication_Errors == strErrorReasonCode {
print("\n\n\n\nauthentication_Errors :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
self.isUnAuthotized()
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(strErrorReasonCode) {
print("\n\n\n\nauthentication_Errors_Range :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
CustomAlert().ShowAlert(responce.message)
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(Alamofire_Error) {
self.POST(urlString, paramaters: paramaters, showLoader: showLoader, success: { (responseObject) in
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
}, failure: {_ in
failure(jsonResponce)
})
}
}
else {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
}
}
}
else {
failure(nil)
}
}
})
case .notAvailable:
if let _ = showLoader {
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kNoInternet, duration: 3.0, position: "bottom" as AnyObject)
}
failure(JSON(AppAlertMsg.kNoInternet))
print("No internet")
}
}
// MARK: - POST Method
func POST(_ urlString: String, paramaters: [String: AnyObject]? = nil, showLoader: Bool? = nil, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
switch checkInternetConnection() {
case .available:
if let showLoader = showLoader {
if showLoader {
DispatchQueue.main.async(execute: {
if !UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.beginIgnoringInteractionEvents()
}
SVProgressHUD.show(withStatus: AppAlertMsg.kPleaseWait)
})
}
}
sharedManager.request(baseUrlDev.add(urlString: urlString), method: .post, parameters: paramaters, encoding: JSONEncoding.default, headers: self.getHeaders(urlString)).validate().responseJSON(completionHandler: { response in
DispatchQueue.main.async {
if UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.endIgnoringInteractionEvents()
}
if let showLoader = showLoader {
if showLoader {
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
}
}
}
//Success
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
//Print response using below method
self.printResponse(urlString: urlString, paramaters: paramaters, response: (value as AnyObject))
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
} else {
//Check response error using status code
if let strErrorReasonCode : Int = response.response?.statusCode {
if let data = response.data {
let jsonResponce = JSON(data: data)
if strErrorReasonCode == 500 {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
return
}
if let dictionary : NSDictionary = jsonResponce.dictionaryObject as NSDictionary? {
let responce : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictionary)
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let Alamofire_Error = -1005
if authentication_Errors == strErrorReasonCode {
print("\n\n\n\nauthentication_Errors (jsonResponce)\n\n\n\n")
self.isUnAuthotized()
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(strErrorReasonCode) {
print("\n\n\n\nauthentication_Errors_Range :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
CustomAlert().ShowAlert(responce.message)
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(Alamofire_Error) {
self.POST(urlString, paramaters: paramaters, showLoader: showLoader, success: { (responseObject) in
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
}, failure: {_ in
failure(jsonResponce)
})
}
}
else {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
}
}
}
else {
failure(nil)
}
}
})
case .notAvailable:
if let _ = showLoader {
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kNoInternet, duration: 3.0, position: "bottom" as AnyObject)
}
failure(JSON(AppAlertMsg.kNoInternet))
print("No internet")
}
}
// MARK: - No Internet Connection
func checkInternetConnection() -> NetworkConnection {
if isNetworkAvailable() {
return .available
}
return .notAvailable
}
// MARK: - Check Status
func isValidated(_ response: AnyObject?) -> (Bool, JSON) {
//Removing null and <null>, and replacing number or integer to string
guard var dictResponse = response as? NSDictionary else{
return (false,nil)
}
dictResponse = (dictResponse.replacingNullsWithStrings() as NSDictionary).convertingNumbersToStrings()! as NSDictionary
let jsonResponce = JSON(dictResponse)
let responseModel : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictResponse)
/* //
HTTP Status Code
200 – Success/OK
4xx – Authentication Errors
5xx – Service Errors
*/ //
guard let statusCodeInt = responseModel.code.toInt() else {
print("code is not proper")
return (false,jsonResponce)
}
let success_Range = 200..<300
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let service_Errors_Range = 500..<600
if success_Range.contains(statusCodeInt) {
// all okey
return (true, jsonResponce)
}
else if authentication_Errors == statusCodeInt {
print("service_Errors_Range :\(statusCodeInt)")
self.isUnAuthotized()
return (false,jsonResponce)
}
else if authentication_Errors_Range.contains(statusCodeInt) {
print("authentication_Errors_Range :\(statusCodeInt)")
CustomAlert().ShowAlert(responseModel.message)
return (false,jsonResponce)
}
else if service_Errors_Range.contains(statusCodeInt) {
print("service_Errors_Range :\(statusCodeInt)")
CustomAlert().ShowAlert(responseModel.message)
return (false,jsonResponce)
}
else {
return (false,nil)
}
}
func isUnAuthotized() {
// we have to logout, bcos session expired , or user unauthorized
CustomAlert().ShowAlert(isCancelButton: false, strMessage: AppAlertMsg.kUnAuthotized) { (isYESClicked) in
if isYESClicked {
//Delete all data from user default
//Set sign in as Home screen
print("set to Login view controller ")
GmailClass.sharedInstance().signOut()
LoginManager().logOut()
UserStatus = UserType.Fresh.rawValue
let deviceTokenTemp = DeviceToken
if let bundle = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundle)
}
DeviceToken = deviceTokenTemp
Constants.appDelegate.logoutSuccess()
}
}
}
// MARK: - Loader method
class func ShowActivityIndicatorInStatusBar() {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
class func HideActivityIndicatorInStatusBar() {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
extension String {
func add(urlString: String) -> URL {
return URL(string: self + urlString)!
}
func EncodingText() -> Data {
return self.data(using: String.Encoding.utf8, allowLossyConversion: false)!
}
}