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

74 lines
2.5 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 showNotification(identifier:String? = nil, title:String?, subtitle:String?, message:String?, sound:UNNotificationSound = UNNotificationSound.default){
let notificationContent = UNMutableNotificationContent()
if identifier != nil { notificationContent.categoryIdentifier = identifier! }
if title != nil { notificationContent.title = title! }
if subtitle != nil { notificationContent.body = message! }
if message != nil { notificationContent.subtitle = subtitle! }
notificationContent.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "\(Date().timeIntervalSinceNow)", content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Error: \(error)")
}
}
}
4 years ago
func httpPostRequest(urlString:String, jsonBody:[String:Any], completion:((Bool,[String:Any]?)->Void)?){
let json: [String: Any] = jsonBody
let jsonData = try? JSONSerialization.data(withJSONObject: json)
4 years ago
// 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
4 years ago
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)
}
}
}
4 years ago
task.resume()
}