-
Notifications
You must be signed in to change notification settings - Fork 7
/
ApiUtils.swift
148 lines (137 loc) · 5.89 KB
/
ApiUtils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// ApiUtils.swift
// Geotify
//
// Created by JaKoB on 22.05.15.
// Copyright (c) 2015 Jakub Josef. All rights reserved.
//
import Foundation
import CoreLocation
struct ApiUtils{
// ENABLE OR DISABLE LOGGING POST REQUESTS
static var kLogging=true
//API URLS
static var kApiBaseURL = "http://pavelbartos.vserver.cz/geofencing/"
static var kApiLoginURL = kApiBaseURL + "login.php"
static var kApiGeotificationsStoreURL = kApiBaseURL + "geotifications.php"
static func doLogin(username:NSString,password:NSString) -> (response:NSHTTPURLResponse,jsonData:NSDictionary?){
var (response,jsonData,requestError, responseError,jsonError) = ApiUtils.makePOST(kApiLoginURL, data: ["username": username, "password": password])
return (response,jsonData)
}
static func storeGeotification(geotification:Geotification,sessionID:NSString){
var (response, jsonData, requestError, responseError,jsonError) = self.makePOST(kApiGeotificationsStoreURL, data: ["action": "store", "sessionID":sessionID,"geotification":geotification.serialize()])
if let data = jsonData{
if let result = data.valueForKey("result") as? String{
if(result != "ok"){
NSLog("Cannot store geotification, server not returns result:ok")
}
}
}else{
NSLog("Cannot store geotification, server returns nothing")
}
//otherwise store is success
if(kLogging){
NSLog("Geotification \(geotification.note) stored")
}
}
static func editGeotification(geotification:Geotification,sessionID:NSString){
var (response, jsonData, requestError, responseError,jsonError) = self.makePOST(kApiGeotificationsStoreURL, data: ["action": "edit", "sessionID":sessionID,"geotification":geotification.serialize()])
if let data = jsonData{
if let result = data.valueForKey("result") as? String{
if(result != "ok"){
NSLog("Cannot edit geotification, server not returns result:ok")
}
}
}else{
NSLog("Cannot edit geotification, server returns nothing")
}
//otherwise store is success
if(kLogging){
NSLog("Geotification \(geotification.note) edited")
}
}
static func deleteGeotification(geotification:Geotification,sessionID:NSString){
var (response, jsonData, requestError, responseError,jsonError) = self.makePOST(kApiGeotificationsStoreURL, data: ["action": "delete", "sessionID":sessionID,"geotification":geotification.serialize()])
if let data = jsonData{
if let result = data.valueForKey("result") as? String{
if(result != "ok"){
NSLog("Cannot delete geotification, server not returns result:ok")
}
}
}else{
NSLog("Cannot delete geotification, server returns nothing")
}
//otherwise store is success
if(kLogging){
NSLog("Geotification \(geotification.note) deleted")
}
}
static func makePOST(url:String, data: NSDictionary) -> (response:NSHTTPURLResponse,jsonData: NSDictionary?, requestError:NSError?, responseError: NSError?,jsonError: NSError?){
var requestError: NSError?
var postData:NSData = NSJSONSerialization.dataWithJSONObject(data, options: nil, error: &requestError)!
if(requestError != nil){
NSLog("Request error: " + requestError!.description)
}
if(kLogging){
NSLog("Making POST request to \(url)")
NSLog("PostData: %@",NSString(data: postData, encoding: NSUTF8StringEncoding)!)
}
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string:url)!)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var responseError: NSError?
var response: NSURLResponse?
var data:NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&responseError)
var res = response as! NSHTTPURLResponse!
var jsonData:NSDictionary?
var jsonError:NSError?
if(kLogging){
if(res != nil){
NSLog("Response code %ld",res.statusCode)
}else{
NSLog("")
}
}
if(data != nil){
var responseData:NSString = NSString(data:data!, encoding:NSUTF8StringEncoding)!
if(kLogging){
NSLog("Response ==> %@", responseData);
}
jsonData = NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers , error: &jsonError) as? NSDictionary
}else{
NSLog("Cannot process POST request, returned data is nil!")
}
if(responseError != nil){
NSLog("Response error: " + responseError!.description)
}
if(jsonError != nil){
NSLog("JSON error: " + jsonError!.description)
}
return (res,jsonData,requestError, responseError, jsonError)
}
// COMMON FUNCTIONS
static func getGeotificationsArrayFromJSONData(jsonData: NSArray) -> NSMutableArray {
var returnItems = NSMutableArray()
for jsonObject in jsonData{
if let jsonDict = jsonObject as? NSDictionary{
//create coordinate object
let coordinate = CLLocationCoordinate2D(latitude: jsonDict["latitude"] as! Double, longitude: jsonDict["longitude"] as! Double)
//create EventType
let eventType:EventType
if(jsonDict["eventType"] as! NSInteger == 0){
eventType=EventType.OnEntry
}else{
eventType=EventType.OnExit
}
let geotification = Geotification(coordinate: coordinate, radius: jsonDict["radius"] as! Double, identifier: jsonDict["identifier"] as! String, note: jsonDict["note"] as! String, eventType: eventType)
//convert and add to return array
returnItems.addObject(NSKeyedArchiver.archivedDataWithRootObject(geotification))
}
}
return returnItems
}
}