This repository was archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAppDelegate.swift
116 lines (93 loc) · 3.97 KB
/
AppDelegate.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
//
// AppDelegate.swift
// WatchKitExample
//
// Created by Boris Bügling on 19/11/14.
// Copyright (c) 2014 Contentful GmbH. All rights reserved.
//
import CoreLocation
import MapKit
import UIKit
typealias WatchKitReply = (([NSObject : AnyObject]!) -> Void)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var bgTask: UIBackgroundTaskIdentifier?
var currentLocationCallback: ((location: CLLocationCoordinate2D) -> Void)?
var locationManager: CLLocationManager?
var reply: WatchKitReply?
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
locationManager = CLLocationManager()
locationManager!.delegate = self;
locationManager!.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager!.distanceFilter = 1000
locationManager!.requestAlwaysAuthorization()
locationManager!.startUpdatingLocation()
return true
}
func applicationDidEnterBackground(application: UIApplication) {
if self.reply == nil {
locationManager?.stopUpdatingLocation()
}
}
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let reply = reply {
self.reply = reply
bgTask = application.beginBackgroundTaskWithExpirationHandler() {
self.endBackgroundTaskForWatchKitExtension()
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self.handleBackgroundLocationRequest()
}
}
}
func endBackgroundTaskForWatchKitExtension() {
self.reply = nil
if self.bgTask == nil || self.bgTask! == UIBackgroundTaskInvalid {
return
}
locationManager?.stopUpdatingLocation()
dispatch_after(2 * dispatch_time(DISPATCH_TIME_NOW, Int64(2 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
UIApplication.sharedApplication().endBackgroundTask(self.bgTask!)
self.bgTask = UIBackgroundTaskInvalid
}
}
func handleBackgroundLocationRequest() {
if let location = locationManager?.location {
var coordinate = location.coordinate
self.reply?(["currentLocation": NSData(bytes: &coordinate, length: sizeof(CLLocationCoordinate2D))])
endBackgroundTaskForWatchKitExtension()
} else {
if !CLLocationManager.locationServicesEnabled() ||
CLLocationManager.authorizationStatus() != .AuthorizedAlways {
self.reply?(nil)
endBackgroundTaskForWatchKitExtension()
}
}
}
func handleLocationUpdate(newLocation: CLLocation) {
var location = newLocation.coordinate
if let currentLocationCallback = currentLocationCallback {
currentLocationCallback(location: location)
}
if let reply = reply {
reply(["currentLocation": NSData(bytes: &location, length: sizeof(CLLocationCoordinate2D))])
endBackgroundTaskForWatchKitExtension()
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
NSLog("Error while determining user location: %@", error)
self.reply?(nil)
endBackgroundTaskForWatchKitExtension()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let newLocation = locations.first as? CLLocation {
handleLocationUpdate(newLocation)
}
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
if let newLocation = newLocation {
handleLocationUpdate(newLocation)
}
}
}