@@ -20,6 +20,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2020 let versionNumber : AnyObject ? = NSBundle . mainBundle ( ) . infoDictionary ? [ " CFBundleVersion " ]
2121 print ( " version \( versionNumber!) " )
2222
23+ UIDevice . currentDevice ( ) . batteryMonitoringEnabled = true
24+
2325 let item = NotificationData ( )
2426 item. alert = " Swift Push ( \( versionNumber!) ) starting on " + UIDevice. currentDevice ( ) . name
2527 notifications. insert ( item, atIndex: 0 )
@@ -60,7 +62,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
6062 let mode = receipt
6163 let versionNumber : AnyObject ? = NSBundle . mainBundle ( ) . infoDictionary ? [ " CFBundleVersion " ]
6264
63- let request = NSMutableURLRequest ( URL: NSURL ( string: " http ://www.trease.eu/ibeacon/swiftpush/" ) !)
65+ let request = NSMutableURLRequest ( URL: NSURL ( string: " https ://www.trease.eu/ibeacon/swiftpush/" ) !)
6466 request. HTTPMethod = " POST "
6567 var bodyData = " token= \( deviceToken. description) "
6668 bodyData += " &device= \( UIDevice . currentDevice ( ) . name) "
@@ -116,6 +118,46 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
116118 let center = NSNotificationCenter . defaultCenter ( )
117119 center. postNotificationName ( " dataChanged " , object: self )
118120
121+
122+
123+ let formatter = NSNumberFormatter ( )
124+ formatter. numberStyle = . PercentStyle
125+
126+ let batteryLevel = formatter. stringFromNumber ( UIDevice . currentDevice ( ) . batteryLevel)
127+
128+ var chargeStatus = " "
129+
130+ switch UIDevice . currentDevice ( ) . batteryState {
131+ case UIDeviceBatteryState . Unknown:
132+ chargeStatus = " Unknown "
133+ case UIDeviceBatteryState . Unplugged:
134+ chargeStatus = " Unplugged "
135+ case UIDeviceBatteryState . Charging:
136+ chargeStatus = " Charging "
137+ case UIDeviceBatteryState . Full:
138+ chargeStatus = " Full "
139+ }
140+
141+ print ( " battery status change: " + chargeStatus + " " + batteryLevel!)
142+
143+ let request = NSMutableURLRequest ( URL: NSURL ( string: " https://www.trease.eu/ibeacon/battery/ " ) !)
144+ request. HTTPMethod = " POST "
145+ var bodyData = " &device= \( UIDevice . currentDevice ( ) . name) "
146+ bodyData += " &batterystate= " + chargeStatus
147+ bodyData += " &batterylevel= " + batteryLevel!
148+ request. HTTPBody = bodyData. dataUsingEncoding ( NSUTF8StringEncoding)
149+
150+ let task = NSURLSession . sharedSession ( ) . dataTaskWithRequest ( request) {
151+ data, response, error in
152+ let x = response as? NSHTTPURLResponse
153+ print ( " status code \( x? . statusCode) " )
154+ }
155+ task!. resume ( )
156+
157+
158+
159+
160+
119161 // finished
120162 completionHandler ( UIBackgroundFetchResult . NewData)
121163 }
0 commit comments