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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Hsu
authored and
John Hsu
committed
May 27, 2013
1 parent
c4886fd
commit df0f1b7
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// JOWaitForLocationUpdate.h | ||
// JOWaitForLocationUpdate | ||
// | ||
// Created by Hsu John on 12/5/17. | ||
// Copyright (c) 2012 All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <CoreLocation/CoreLocation.h> | ||
void JOWaitForLocationUpdate(NSTimeInterval interval,void (^succeedBlock)(CLLocation *location) ,void (^timeoutBlock)(void) ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |