YHNaviteLBS.m
6.93 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// YHNaviteLBS.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 4/10/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHNaviteLBS.h"
#import <CoreLocation/CoreLocation.h>
NSString * const YHNative_LBS = @"Native_LBS";
@interface YHNaviteLBS ()
<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (nonatomic) BOOL locationStarted;
@property (strong, nonatomic) CLGeocoder *geocoder;
@end
@implementation YHNaviteLBS
- (NSString *)actionName
{
return YHNative_LBS;
}
- (void)dealloc
{
if (_geocoder) {
[_geocoder cancelGeocode];
_geocoder = nil;
}
}
- (BOOL)isAuthorized
{
BOOL authorizationStatusClassPropertyAvailable = [CLLocationManager respondsToSelector:@selector(authorizationStatus)]; // iOS 4.2+
if (authorizationStatusClassPropertyAvailable) {
NSUInteger authStatus = [CLLocationManager authorizationStatus];
#ifdef __IPHONE_8_0
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { //iOS 8.0+
return (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) || (authStatus == kCLAuthorizationStatusAuthorizedAlways) || (authStatus == kCLAuthorizationStatusNotDetermined);
}
#endif
return (authStatus == kCLAuthorizationStatusAuthorized) || (authStatus == kCLAuthorizationStatusNotDetermined);
}
// by default, assume YES (for iOS < 4.2)
return YES;
}
- (BOOL)isLocationServicesEnabled
{
BOOL locationServicesEnabledInstancePropertyAvailable = [self.locationManager respondsToSelector:@selector(locationServicesEnabled)]; // iOS 3.x
BOOL locationServicesEnabledClassPropertyAvailable = [CLLocationManager respondsToSelector:@selector(locationServicesEnabled)]; // iOS 4.x
if (locationServicesEnabledClassPropertyAvailable) { // iOS 4.x
return [CLLocationManager locationServicesEnabled];
} else if (locationServicesEnabledInstancePropertyAvailable) { // iOS 2.x, iOS 3.x
return [(id)self.locationManager locationServicesEnabled];
} else {
return NO;
}
}
- (void)startLocation:(BOOL)enableHighAccuracy
{
if (![self isLocationServicesEnabled]) {
self.failureCallBack(@"Location services are not enabled.");
return;
}
if (![self isAuthorized]) {
NSString* message = nil;
BOOL authStatusAvailable = [CLLocationManager respondsToSelector:@selector(authorizationStatus)]; // iOS 4.2+
if (authStatusAvailable) {
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined) {
// could return POSITION_UNAVAILABLE but need to coordinate with other platforms
message = @"User undecided on application's use of location services.";
} else if (code == kCLAuthorizationStatusRestricted) {
message = @"Application's use of location services is restricted.";
} else if (code == kCLAuthorizationStatusDenied) {
message = @"Application's use of location services is denied.";
}
}
// PERMISSIONDENIED is only PositionError that makes sense when authorization denied
self.failureCallBack(message);
return;
}
#ifdef __IPHONE_8_0
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) { //iOS8+
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSString *message = @"[Warning] No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file.";
self.failureCallBack(message);
}
return;
}
#endif
[self.locationManager stopUpdatingLocation];
[self.locationManager startUpdatingLocation];
self.locationStarted = YES;
}
- (void)stopLocation
{
if (self.locationStarted) {
if (![self isLocationServicesEnabled]) {
return;
}
[self.locationManager stopUpdatingLocation];
self.locationStarted = NO;
}
}
- (NSArray *)availableFunctions
{
return @[ @"getLocation", @"getLocationDetail" ];
}
- (void)getLocation
{
if (!_locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationStarted = NO;
}
if (![self isLocationServicesEnabled]) {
self.failureCallBack(@"Location services are disabled.");
} else {
[self startLocation:YES];
}
}
- (void)getLocationDetail
{
if (!_geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
[self getLocation];
}
- (void)reverseGeoCode:(CLLocation *)location
{
__weak typeof(self)weakSelf = self;
[self.geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
weakSelf.failureCallBack([error description]);
} else {
CLPlacemark *pm = [placemarks lastObject];
weakSelf.successCallBack(pm.addressDictionary, NO);
}
weakSelf.geocoder = nil;
}];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
[self stopLocation];
CLLocation *location = locations.lastObject;
if (self.geocoder) {
[self reverseGeoCode:location];
} else {
self.successCallBack(@{ @"latitude":@(location.coordinate.latitude),
@"longitude":@(location.coordinate.longitude) }, NO);
}
}
- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error
{
NSLog(@"locationManager::didFailWithError %@", [error localizedFailureReason]);
self.failureCallBack([error localizedDescription]);
if (error.code != kCLErrorLocationUnknown) {
[self.locationManager stopUpdatingLocation];
self.locationStarted = NO;
}
}
//iOS8+
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if(!self.locationStarted){
[self startLocation:YES];
}
}
@end