Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Adds JOWaitForLocationUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
John Hsu authored and John Hsu committed May 27, 2013
1 parent c4886fd commit df0f1b7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
12 changes: 12 additions & 0 deletions JOWaitForLocationUpdate.h
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) );

110 changes: 110 additions & 0 deletions JOWaitForLocationUpdate.m
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();
}
}

0 comments on commit df0f1b7

Please sign in to comment.