You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PatientApp-KKUMC/ios/Runner/Helper/GlobalHelper.swift

56 lines
1.6 KiB
Swift

4 years ago
//
// GlobalHelper.swift
// Runner
//
// Created by ZiKambrani on 29/03/1442 AH.
//
import UIKit
4 years ago
func dictionaryArray(from:String) -> [[String:Any]]{
if let data = from.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] ?? []
} catch {
print(error.localizedDescription)
}
}
return []
}
func httpPostRequest(urlString:String, jsonBody:[String:Any], completion:((Bool,[String:Any]?)->Void)?){
let json: [String: Any] = jsonBody
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("*/*", forHTTPHeaderField: "Accept")
4 years ago
request.httpMethod = "POST"
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any], let status = responseJSON["MessageStatus"] as? Int{
print(responseJSON)
if status == 1{
completion?(true,responseJSON)
}else{
completion?(false,responseJSON)
}
}
}
task.resume()
}