I've created a helper class for my location needs so i don't violate the DRY principle. The class looks like this:
Location.h
#interface Location : NSObject <CLLocationManagerDelegate>{
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
-(float)latitude;
-(float)longitude;
-(NSString *)postalcode;
Location.m
#implementation Location{
float latitude;
float longitude;
NSString *postalcode;
}
-(id)init{
NSLog(#"Hallo");
[self setupLocationManager];
return self;
}
-(float)latitude{
return latitude;
}
-(float)longitude{
return longitude;
}
-(NSString *)postalcode{
return postalcode;
}
-(void)setupLocationManager{
manager = [[CLLocationManager alloc] init];
[manager requestWhenInUseAuthorization];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.distanceFilter = 100;
[manager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init];
}
#pragma mark - CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error
{
NSLog(#"Error: %#", error);
NSLog(#"Failed to get location! :(");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Location: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
latitude = currentLocation.coordinate.latitude;
longitude = currentLocation.coordinate.longitude;
}
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
postalcode = [NSString stringWithFormat:#"%#",placemark.postalCode];
/*
self.address.text = [NSString stringWithFormat:#"%# %#\n%# %#\n%#\n%#",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
*/
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
#end
When i in my ViewController tries to create an instance of Location and set latitude and longitude labels, in the viewDidLoad method, the labels are sat to 0.00000.
Apparently it takes around half a second for Location to get the coordinates.
I've tried using
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self setCoordinateLabels];
});
But that just seems very hacky and can't possibly be the best practise? So is there any way i could do this a better way?
Well - that is very hacky. Why don't you forward your delegate methods call?
locationManager:didUpdateToLocation:
(Btw. this is a legacy function)
Tells you when the first location is set. You can just have an array of delegates on your Location class and call every delegate when it's time.
Here is an example with blocks:
static NSMapTable *listenerBlocks;
+ (void)addListener:(NSObject *)listener listenerBlock:(void (^)())listenerBlock
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
listenerBlocks =
[NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory
valueOptions:NSMapTableStrongMemory];
});
if (listenerBlock && listener) {
[listenerBlocks setObject:listenerBlock forKey:listener];
}
}
+ (void)removeListener:(NSObject *)listener
{
if (listener) {
[listenerBlocks removeObjectForKey:listener];
}
}
In your locationManager:didUpdateToLocation: you then just call
NSArray *allBlocks = [[listenerBlocks objectEnumerator] allObjects];
for(void (^listenerBlock)(NSString *) in allBlocks)
{
listenerBlock();
}
at the end
In the class that needs updates for the labels (e.g. myLabel):
[Location addListener:self listenerBlock:^{
dispatch_async(dispatch_get_main_queue(),^{
//myLabel.text = //... update your label here
[self setCoordinateLabels]; // as from your code above..
});
}];
Related
I'm trying to implement longitude and latitude in my sample app. I use iPhone 5s device to test it. It doesn't show any effect on latitude and longitude labels on button actions.
Before compiling Xcode shows warning at line didUpdateToLocation: fromLocation: method as
"implementing deprecated method"
please help me with the suitable method which replaces this method for smooth working of the app
With Xcode 12.4 and for iOS 12.3 using objective c I'm trying to implement a simple location demo app which shows longitude and latitude. Below are my viewController.h and viewController.m files. And I've tried this below code from online tutorial AppCoda
viewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController :UIViewController<CLLocationManagerDelegate>
{
IBOutlet UILabel *lattitudeLabel;
IBOutlet UILabel *longitudeLabel;
IBOutlet UILabel *addressLabel;
}
- (IBAction)getCurrentLocation:(UIButton *)sender;
#end
viewController.m
#import "ViewController.h"
#interface ViewController ()
{
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc]init];
geocoder = [[CLGeocoder alloc] init];
}
- (IBAction)getCurrentLocation:(UIButton *)sender
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
NSLog(#"Error: Failed to get location");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
lattitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
[locationManager stopUpdatingLocation];
NSLog(#"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"Found placemarks: %#, error: %#", placemarks, error);
if (error == nil && [placemarks count] > 0) {
self->placemark = [placemarks lastObject];
self->addressLabel.text = [NSString stringWithFormat:#"%# %#\n%# %#\n%#\n%#",
self->placemark.subThoroughfare, self->placemark.thoroughfare,
self->placemark.postalCode, self->placemark.locality,
self->placemark.administrativeArea,
self->placemark.country];
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
#end
On clicking button get my location nothing is happening. It should actually update the co-ordinates besides Longitude: and Latitude: some float values..but it's not doing that..I'll ask please suggest me links to any tutorial web site or any GitHub project for location handling in iOS 12.3 and above in objective c...Thanks for the replies and suggested edits.
As the docs say, just use locationManager(_:didUpdateLocations:)
.
Swift 5
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Stop updates if this is a one-time request
manager.stopUpdatingLocation()
// Pop the last location off if you just need current update
guard let newLocation = locations.last else { return }
<... Do something with the location ...>
}
Objective-C
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[manager stopUpdatingLocation];
CLLocation *newLocation = [locations lastObject];
if (newLocation) {
<... Do something with the location ...>
}
}
I hope this implementation for delegate method will help you.
works both on Mac and IOS. I used it to pinpoint current position for weather application.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
if (locations.count > 0) {
// Location manager has nonempty array of readings
// We take the las one - the latest position determined
CLLocation *last = locations[locations.count - 1];
// Evaluate threshold distance to notice change
CLLocationDistance distance = LOCATOR_SENSITIVITY + 1;
if (previous) {
// calculate distance from previous location
distance = [last distanceFromLocation:previous];
}
// update previous location
previous = last;
if (distance > LOCATOR_SENSITIVITY) {
// If we moved far enough, update current position
currentLat = last.coordinate.latitude;
currentLon = last.coordinate.longitude;
// and notify all observers
// You can use delegate here, dispatch_async wrapper etc.
[[NSNotificationCenter defaultCenter]
postNotificationName:LocationIsChanged object:nil];
}
}
}
For getting a location, I made LocationManager.h and LocationManager.m
LocationManager.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface LocationManager : NSObject <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *clLocationMgr;
#property (strong, nonatomic) CLLocation *clLocation;
#property float latitude;
#property float longitude;
+ (LocationManager*)getSharedInstance;
- (void)startLocation;
- (float)currentLatitude;
- (float)currentLogitude;
- (NSString*)abbreviatedDistance:(int)_distance;
#end
LocationManager.m
#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>
#implementation LocationManager
static LocationManager *sharedInstance = nil;
+ (LocationManager *) getSharedInstance {
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
- (CLLocationManager *)getLocationManager {
if (_clLocationMgr == nil) {
_clLocationMgr = [[CLLocationManager alloc] init];
}
[_clLocationMgr setDelegate: self];
return _clLocationMgr;
}
- (void) startLocation {
if (_clLocationMgr == nil) {
_clLocationMgr = [self getLocationManager];
}
[_clLocationMgr setDistanceFilter: kCLDistanceFilterNone];
[_clLocationMgr setDesiredAccuracy: kCLLocationAccuracyBest];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(#"location service not available");
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted ||
status == kCLAuthorizationStatusDenied) {
NSLog(#"location service is restriced or is denied");
}
[_clLocationMgr startUpdatingLocation];
_clLocation = [_clLocationMgr location];
_latitude = _clLocation.coordinate.latitude;
_longitude = _clLocation.coordinate.longitude;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
_clLocation = [locations lastObject];
_latitude = _clLocation.coordinate.latitude;
_longitude = _clLocation.coordinate.longitude;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"Fail to handle location: %#", error);
if (![CLLocationManager locationServicesEnabled]) {
NSLog(#"location service not available");
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
NSLog(#"location service is restriced or is denied");
}
}
- (float)currentLatitude {
return _latitude;
}
- (float)currentLogitude {
return _longitude;
}
- (NSString *)abbreviatedDistance:(int)_distance {
if(_distance < 1000) {
return [NSString stringWithFormat:#"%#m", [[NSNumber numberWithInt:_distance] stringValue]];
} else {
double distanceDouble = _distance / 1000;
return [NSString stringWithFormat:#"%#km", [[NSNumber numberWithDouble:distanceDouble] stringValue]];
}
}
#end
And MainViewController.m call location manager.
- (void)viewDidLoad {
[super viewDidLoad];
[[LocationManager getSharedInstance] startLocation];
}
When I install my app at the first time, location is 0.00000.
I don't have any idea why location is like that.
Is there any problem with the code?
I can remember i read somewhere that you will get imediatly the last known location and then updates of new locations.
in this article Getting the User’s Current Location | Receiving Location Data from a Service apple recommends to check the age of the received data:
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// If the event is recent, do something with it.
NSLog(#"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
}
}
i think this way you can filter the "invalid" location updates and the "zero update".
i want to find the current location of the user
below the code for finding coordinates from the well defined adress
hom can i find the current location of the user ..please help me
.h file
#import
#import
#interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
#property(nonatomic,retain) MKMapView *myMapView;
#property (nonatomic, strong) CLGeocoder *myGeocoder;
#end
.m file
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#interface ViewController ()
{
CLLocationManager *locationManager;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.myMapView.showsUserLocation=YES;
self.view.backgroundColor=[UIColor whiteColor];
self.myMapView=[[MKMapView alloc]initWithFrame:self.view.bounds];
self.myMapView.mapType=MKMapTypeHybrid;
self.myMapView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myMapView];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
NSString *oreillyAddress =#"india";
self.myGeocoder = [[CLGeocoder alloc] init];
[self.myGeocoder
geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count] > 0 && error == nil)
{
NSLog(#"Found %lu placemark(s).", (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; NSLog(#"Longitude = %f", firstPlacemark.location.coordinate.longitude); NSLog(#"Latitude = %f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] == 0 &&
error == nil){ NSLog(#"Found no placemarks.");
}
else if (error != nil){
NSLog(#"An error occurred = %#", error); }
}];
// Do any additional setup after loading the view, typically from a nib.
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
float latitude=newLocation.coordinate.latitude;
float longitude=newLocation.coordinate.longitude;
NSLog(#"%f",latitude);
NSLog(#"%f",longitude);
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(#"error==>%#",error);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Define
CLLocationManager *lm;
CLLocation *location;
and then add the below mentioned code in your ViewDidLoad method
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
location = [lm location];
CLLocationCoordinate2D coordinate;
coordinate.longitude = location.coordinate.longitude;
coordinate.latitude = location.coordinate.latitude;
if((location.coordinate.longitude== 0.0 ) && (location.coordinate.latitude==0.0))
{
UIAlertView *alert2 = [[UIAlertView alloc ] initWithTitle:(#"Server Error:")message:(#"Internet Problem. Try Later !!!") delegate:nil cancelButtonTitle:nil otherButtonTitles:(#"OK"), nil];
[alert2 show];
}
else
{
coordinate = [location coordinate];
NSLog(#"Latitude of User is %f",coordinate.longitude);
NSLog(#"Longitude of User is %f",coordinate.latitude);
}
Use this
self.myMapView.showsUserLocation = YES;
If you are using the simulator, you would need to go to: Debug -> Location -> Customer. From there set your location to the desired test point.
To get user's current location in iOS 8
Add <CLLocationManagerDelegate> in .h file.
CLLocationManager *locationManager;
CLLocation *currentLocation;
locationManager = [CLLocationManager new];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse)
{
[locationManager requestAlwaysAuthorization];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
NSString *currentLatitude = [NSString stringWithFormat:#"%f",location.coordinate.latitude];
NSString *currentLongitude = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status)
{
case kCLAuthorizationStatusNotDetermined:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
{
[locationManager requestAlwaysAuthorization];
}
break;
default:
{
[locationManager startUpdatingLocation];
}
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Error while getting core location : %#",[error localizedFailureReason]);
if ([error code] == kCLErrorDenied)
{
//you had denied
}
[manager stopUpdatingLocation];
}
And In your plist file Dont forget to Enter a key
NSLocationAlwaysUsageDescription
Use this:
[self.mapview setShowsUserLocation:YES];
i am using this code to get the Location ,using simulator , but its not giving me any output .
also if someone suggest me a solution of this or a better alternative solution.\
-(void)viewDidAppear:(BOOL)animated
{
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: _locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
if (error) {
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSString *Countrynsme = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCountryKey];
_requestorAddressText.Text = address;
_requestorCityText.text = city;
_requestorPostalText.text = zip;
_CountryrequestorText.text = Countrynsme;
_requestorStateText.text = state;
}
}];
[_locationManager stopUpdatingLocation];
}
CLLocationManager is an asynchronous API. You need to wait for the result of CLLocationManager before you geocode the location.
Start listening for location manager updates using the CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
if (interval < 0) {
interval = -interval;
}
// Reject stale location updates.
if (interval < 30.0) {
// Start geocoding
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// Use geocoded results
...
}];
}
// If you're done with getting updates then do [manager stopUpdatingLocation]
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
// Handle error. Perhaps [manager stopUpdatingLocation]
}
Then viewDidAppear just bootstrap's the location lookup:
- (void)viewDidAppear {
// PS: You're missing a call to [super viewDidAppear]
[super viewDidAppear];
// Start lookup for location
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
}
PS: In dealloc don't forget to stop updating location, cancel geocode and nil the delegates for locationManager.
I have a custom navbar with a locateButton that is visible in every view
When a user taps the locateButton, I would like to update lat and long on every view. I've got it working in one view at a time, with the following code.
ViewController.m
- (UIBarButtonItem *)locateButton {
[locationButton addTarget:self action:#selector(locationPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)locationPressed:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
latLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
longLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(#"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"Found placemarks: %#, error: %#", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
addressLabel.text = [NSString stringWithFormat:#"%#, %#",
placemark.locality,
placemark.administrativeArea];
addressLabel.numberOfLines = 0;
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
I'm trying to add a singleton to accomplish this.
In Location.h
// Location.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface Location : NSObject <CLLocationManagerDelegate>
#property (nonatomic, strong) CLLocationManager* locationManager;
+ (Location*)sharedSingleton;
#end
Then in Location.M
// Location.m
#import "Location.h"
#implementation Location
#synthesize locationManager;
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setHeadingFilter:kCLHeadingFilterNone];
[self.locationManager startUpdatingLocation];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//do any more customization to your location manager
}
return self;
}
+ (Location*)sharedSingleton {
static Location* sharedSingleton;
if(!sharedSingleton) {
#synchronized(sharedSingleton) {
sharedSingleton = [Location new];
}
}
return sharedSingleton;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
[locationManager stopUpdatingLocation];
}
#end
How do I get my - (IBAction)locationPressed:(id)sender in another view let's say HomeViewController.m to call this locationManager method and also update the singleton value?
You might consider a singleton as your location manager and location manager delegate. There are a bunch of stackoverflow questions about creating singletons like this one: Singleton in iOS 5?.
Then when your user clicks that button, your singleton class will be called and updated and other views will look at that same singleton to get their location info.