-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkManager.swift
84 lines (70 loc) · 2.56 KB
/
NetworkManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// NetworkManager.swift
// Prism Niligo
//
// Created by amirhosein on 5/9/1396 AP.
// Copyright © 1396 amirhosein. All rights reserved.
//
import Foundation
class NetworkManager {
func networkCall(_ sender:AnyObject ,_ destinationURL : String ,_ serverDictionary: [String: AnyObject]) -> Bool {
var errorFlag = false
var urlString = URL(string : destinationURL )
let session = URLSession.shared
var request = URLRequest(url : urlString! )
request.httpMethod = "POST" ;
do {
request.httpBody = try JSONSerialization.data(withJSONObject: serverDictionary, options: .prettyPrinted)
}
catch let error {
print(error.localizedDescription)
errorFlag = true
return false
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
print(error ?? 0)
errorFlag = true
return
}
guard let data = data else {
errorFlag = true
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data , options: .mutableContainers ) as? [ String: Any ] {
print("RESPONSEEE")
if(destinationURL == "http://192.168.1.40/WebServices/Core.svc/GetState" ){
var lampConfig = lampTabBarController()
lampConfig = sender as! lampTabBarController
lampConfig.manageResponse(json)
return
}
else if (destinationURL == "http://192.168.1.40/WebServices/Profile.svc/Login"){
var loginViewController = loginVC()
loginViewController = sender as! loginVC
loginViewController.manageResponse(json)
}
}
} catch let error {
print("!! DATA FROM SERVER ERROR !! ")
print(error.localizedDescription)
errorFlag = true
return
}
})
task.resume()
return true
// if(errorFlag){
// print("network call error flag")
// return false
// }else {
// return true
// }
}
func parseJSONFromServer(json : Dictionary<String , Any>, dataModel : lampTabBarController ) {
}
}