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.
86 lines
2.8 KiB
Swift
86 lines
2.8 KiB
Swift
//
|
|
// GlobalHelper.swift
|
|
// Runner
|
|
//
|
|
// Created by ZiKambrani on 29/03/1442 AH.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
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 dictionary(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 nil
|
|
|
|
}
|
|
|
|
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)")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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")
|
|
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()
|
|
|
|
}
|