Created
September 7, 2014 22:19
-
-
Save weixiyen/7dff79d1329c514628d7 to your computer and use it in GitHub Desktop.
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
// | |
// FFVerifyLocationViewModel.m | |
// Blitz | |
// | |
// Created by Sunny Kang on 9/1/14. | |
// Copyright (c) 2014 Blitz. All rights reserved. | |
// | |
#import "FFVerifyLocationViewModel.h" | |
#import <CoreLocation/CoreLocation.h> | |
#import <AddressBook/AddressBook.h> | |
@interface FFVerifyLocationViewModel() <CLLocationManagerDelegate> | |
@property (nonatomic, strong) CLLocationManager *locationManager; | |
@end | |
@implementation FFVerifyLocationViewModel | |
#pragma mark - external methods to call from view | |
- (void)verifyLocation { | |
[self startSignificantChangeUpdates]; | |
} | |
#pragma mark - internal location methods | |
- (void)startSignificantChangeUpdates | |
{ | |
if ([CLLocationManager locationServicesEnabled]) | |
{ | |
if (!self.locationManager) | |
self.locationManager = [[CLLocationManager alloc] init]; | |
self.locationManager.delegate = self; | |
[self.locationManager startMonitoringSignificantLocationChanges]; | |
} | |
} | |
- (void)stopSignificantChangesUpdates | |
{ | |
[self.locationManager stopUpdatingLocation]; | |
self.locationManager = nil; | |
} | |
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations | |
{ | |
CLLocation *location = [locations lastObject]; | |
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; | |
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { | |
CLPlacemark *placemark = placemarks[0]; | |
NSDictionary *addressDictionary = [placemark addressDictionary]; | |
self.city = addressDictionary[(NSString *)kABPersonAddressCityKey]; | |
self.state = addressDictionary[(NSString *)kABPersonAddressStateKey]; | |
self.country = placemark.country; | |
self.isValidCity = YES; | |
}]; | |
[self stopSignificantChangesUpdates]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment