-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRALocationKitClient.swift
133 lines (113 loc) · 4.96 KB
/
RALocationKitClient.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
//
// RALocationKitClient.swift
// RALocationKitLab
//
// Created by Marcus Ronélius on 2016-01-13.
// Copyright © 2016 Ronelium Applications. All rights reserved.
//
import Foundation
import CoreLocation
class RALocationKitClient: NSObject, CLLocationManagerDelegate {
lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
return locationManager
}()
// Completion handler is used to report back to RALocationKitConvenience
typealias locationCompletionHandler = (location: CLLocation?, error: RALocationKitError?) -> Void
var completionHandler: locationCompletionHandler?
// locationManagers requestLocation seems to be fireing multiple times. we do not want that
var locationsReceived: Bool = false
// Accuracy that is set at the interface.
var accuracy: CLLocationAccuracy?
// Shared instance
func sharedInstance() -> RALocationKitClient {
struct Singleton {
static var sharedInstance = RALocationKitClient()
}
return Singleton.sharedInstance
}
override init() {
super.init()
self.locationManager.delegate = self
// Set the accuracy if its available, otherwise set it to nearest ten meters
if let accuracy = accuracy {
self.locationManager.desiredAccuracy = accuracy
} else {
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
}
}
func updateLocation(completion: locationCompletionHandler) throws -> Void {
completionHandler = completion
let authorization = self.isAuthorized()
// 0 = Authorized, 1 = CLAuthorizationStatus
if authorization.0 {
self.locationManager.requestLocation()
} else {
// Not authorized
if authorization.1 == RALocationKitClient.AuthorizationStatus.NotDetermined {
if (NSBundle.mainBundle().objectForInfoDictionaryKey("NSLocationAlwaysUsageDescription") != nil) {
self.locationManager.requestAlwaysAuthorization()
return
} else if (NSBundle.mainBundle().objectForInfoDictionaryKey("NSLocationWhenInUseUsageDescription") != nil) {
self.locationManager.requestWhenInUseAuthorization()
return
} else {
print("\nRALocationKit:\nPlease set option\n\"NSLocationWhenInUseUsageDescription\"\nor\n\"NSLocationAlwaysUsageDescription\"\nin your info.plist before use\n")
}
}
// Throw error since user has probably choosen not to accept location tracking
throw RALocationKitError.BadAuthorization(authorization.1)
}
}
// MARK: CLLocationManagerDelegate methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
if !self.locationsReceived {
self.locationsReceived = true
completionHandler!(location: location, error: nil)
self.locationManager.stopUpdatingLocation()
}
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
self.locationManager.stopUpdatingLocation()
completionHandler!(location: nil, error: generatedLocationError(error))
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
let authorization = self.isAuthorized()
// 0 = Authorized, 1 = CLAuthorizationStatus
if authorization.0 {
self.locationManager.requestLocation()
}
// Not authorized. If not determine, ask for authorization. Else return error with completionHandler
switch authorization.1 {
case .NotDetermined:
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
break
case .Working:
break
default:
let authParam: [String : AnyObject] = [
"AuthorizationStatus": "\(authorization.1)"
]
completionHandler!(location: nil, error: generatedLocationError(NSError(domain: "AuthorizationStatus", code: 0, userInfo: authParam)))
}
}
/* Helper: Check if we are authorized to perform locationupdates */
private func isAuthorized() -> (Bool, AuthorizationStatus) {
let status = CLLocationManager.authorizationStatus()
switch status {
case .Restricted:
return (false, .Restricted)
case .Denied:
return (false, .Denied)
case .NotDetermined:
return (false, .NotDetermined)
case .AuthorizedWhenInUse:
return (true, .Working)
default:
return (true, .Working)
}
}
}