Skip to content

Instantly share code, notes, and snippets.

@weixiyen
Created September 7, 2014 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weixiyen/7dff79d1329c514628d7 to your computer and use it in GitHub Desktop.
Save weixiyen/7dff79d1329c514628d7 to your computer and use it in GitHub Desktop.
//
// 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