0% found this document useful (0 votes)
33 views4 pages

Nsobject: Import Class Class Var Struct Static Var If Nil

The document defines a WebserviceManager class that manages web service requests. The class has a shared singleton instance, and methods to make POST requests to a URL path by passing parameters, headers, and success/failure callbacks. It also has a helper method to convert a dictionary to JSON data for request bodies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Nsobject: Import Class Class Var Struct Static Var If Nil

The document defines a WebserviceManager class that manages web service requests. The class has a shared singleton instance, and methods to make POST requests to a URL path by passing parameters, headers, and success/failure callbacks. It also has a helper method to convert a dictionary to JSON data for request bodies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

import UIKit

class WebserviceManager: NSObject {

class var sharedInstance: WebserviceManager {


struct Static {
static var instance: WebserviceManager?
}
if Static.instance == nil {
Static.instance = WebserviceManager()
}

return Static.instance!
}

func requestForPost(path: String, param: [String:


Any]?, httpMethod: String, includeHeader: Bool,
success:@escaping (_ response: [String: Any]) ->
Void, failure:@escaping (_ response: [String: Any], _
error: Error?) -> Void) {

let completeURL = Constatnt.urlPath + path

let url = URL(string: completeURL)!


let request = NSMutableURLRequest(url: url)
request.httpMethod = httpMethod
request.httpBody =
ConvertDictionaryToJsonData(object: param)

if includeHeader {
request.setValue("application/json",
forHTTPHeaderField: "Content-Type")
if path != "oauth" {
request.setValue(Constatnt.autKey,
forHTTPHeaderField: "Authorization")
request.setValue("1",
forHTTPHeaderField: "ZRAY-DISABLE")
}
}

var session = URLSession.shared


let urlconfig =
URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 60.0
urlconfig.timeoutIntervalForResource = 60.0
session = URLSession(configuration: urlconfig)

let task = session.dataTask(with: request as


URLRequest) { data, _, error in
if error != nil {
let responseString = String(data: data!,
encoding: String.Encoding.utf8)
print(responseString ?? "")
failure(["title": "Error", "message":
"Something went wrong."], responseString as?
Error)
}
do {
if let responseDictionary = try
JSONSerialization.jsonObject(with: data!, options:
[]) as? NSDictionary {
//print(responseDictionary)
if path == "oauth" {
success((responseDictionary as?
[String : Any])!)
}
if let status =
responseDictionary.value(forKey: "code") {
if "\(status)" == "1" {
success((responseDictionary as?
[String : Any])!)
} else {
let responseString = String(data:
data!, encoding: String.Encoding.utf8)
print(responseString ?? "")
success((responseDictionary as?
[String : Any])!)
}
}
}
} catch {
let responseString = String(data: data!,
encoding: String.Encoding.utf8)
print(responseString ?? "")
failure(["title": "Error", "message":
"Something went wrong."], responseString as?
Error)
}
}
task.resume()
}

func ConvertDictionaryToJsonData(object:
[String: Any]?) -> Data {
var jsonData: Data = Data()
do {
jsonData = try
JSONSerialization.data(withJSONObject: object ??
"", options: .prettyPrinted)
} catch let error as NSError {

print(error)
}
return jsonData
}
}

You might also like