This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JOWaitForLocationUpdate.m
110 lines (101 loc) · 3.19 KB
/
JOWaitForLocationUpdate.m
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
//
// JOWaitForLocationUpdate.m
// JOWaitForLocationUpdate
//
// Created by Hsu John on 12/5/17.
// Copyright (c) 2012 All rights reserved.
//
#import "JOWaitForLocationUpdate.h"
@interface JOWaitForLocationUpdateClass : NSObject <CLLocationManagerDelegate>
{
@private
CLLocationManager *locationManager;
NSTimer *countdownTimer;
}
-(void)setupTimerAndStartCountDown;
-(void)timeup;
@property(nonatomic,copy)void(^succeedBlock)(CLLocation *userLocation);
@property(nonatomic,copy)void(^timeoutBlock)(void);
@property(nonatomic,assign)NSTimeInterval maxWaitingTime;
@end
@implementation YKWaitForLocationUpdateClass
@synthesize succeedBlock,timeoutBlock,maxWaitingTime;
- (void)dealloc
{
[locationManager release];
[countdownTimer invalidate];
countdownTimer = nil;
[super dealloc];
}
- (id)init
{
self = [super init];
if (self) {
YKValidationCode
locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
locationManager.delegate = self;
}
return self;
}
-(void)setupTimerAndStartCountDown
{
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:maxWaitingTime target:self selector:@selector(timeup) userInfo:nil repeats:NO];
}
-(void)timeup
{
[countdownTimer invalidate];
countdownTimer = nil;
if (locationManager.location) {
self.succeedBlock(locationManager.location);
}
else {
self.timeoutBlock();
}
self.succeedBlock = nil;
self.timeoutBlock = nil;
[locationManager stopUpdatingLocation];
[self autorelease];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (countdownTimer) {
[countdownTimer invalidate];
countdownTimer = nil;
}
if (status == kCLAuthorizationStatusAuthorized) {
[self setupTimerAndStartCountDown];
}
else if (status == kCLAuthorizationStatusNotDetermined) {
//do nothing
}
else {
// SimpleAlert(@"您尚未開啟定位服務,請至系統設定中開啟");
self.timeoutBlock();
self.succeedBlock = nil;
self.timeoutBlock = nil;
[self autorelease];
}
}
@end
void YKWaitForLocationUpdate(NSTimeInterval interval,void (^succeedBlock)(CLLocation *location) ,void (^timeoutBlock)(void) )
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
YKWaitForLocationUpdateClass *instance = [[YKWaitForLocationUpdateClass alloc] init];
instance.succeedBlock = succeedBlock;
instance.timeoutBlock = timeoutBlock;
instance.maxWaitingTime = interval;
[instance setupTimerAndStartCountDown];
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
YKWaitForLocationUpdateClass *instance = [[YKWaitForLocationUpdateClass alloc] init];
instance.succeedBlock = succeedBlock;
instance.timeoutBlock = timeoutBlock;
instance.maxWaitingTime = interval;
// wait for auth change and goto location manager delegate
}
else {
// SimpleAlert(@"您尚未開啟定位服務,請至系統設定中開啟");
timeoutBlock();
}
}