I'm trying to integrate the CLLocationManager (MyCLController) from Apple's LocateMe sample into my app, and having problems accessing its data from my main class (AppDelegate).
Within the MyCLController class, I have added a variable called "currLoc" which I update every time a location change is detected (not sure if this is the best way to do this). I'd like to be able to get the value of this variable from anywhere in my App as needed. Not sure if i missed if something like this is already built in.
When I attempt to get currLoc from AppDelegate, my app crashes... I just started and barely understand Objective C, so if you've got a fix, please dumb it down as much as possible. I've tried my best to understand Apple's The Objective-C 2.0 Programming Language sections on Classes/Properties, etc, but am having no luck.
Code excerpts below.
Within the MyCLController class, I have added a variable called "currLoc" which I update every time a location change is detected (not sure if this is the best way to do this). I'd like to be able to get the value of this variable from anywhere in my App as needed. Not sure if i missed if something like this is already built in.
When I attempt to get currLoc from AppDelegate, my app crashes... I just started and barely understand Objective C, so if you've got a fix, please dumb it down as much as possible. I've tried my best to understand Apple's The Objective-C 2.0 Programming Language sections on Classes/Properties, etc, but am having no luck.
Code excerpts below.
Code:
/****** File: MyCLController.h ****/
// This protocol is used to send the text for location updates back to another view controller
@protocol MyCLControllerDelegate <NSObject>
@required
-(void)newLocationUpdate:(NSString *)text;
-(void)newError:(NSString *)text;
@end
// Class definition
@interface MyCLController : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
id delegate;
[B] NSString *currLoc;[/B]
}
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic,assign) id <MyCLControllerDelegate> delegate;
[B]@property (readonly) NSString *currLoc;[/B]
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
+ (MyCLController *)sharedInstance;
@end
Code:
/****** File: MyCLController.m *****/
@implementation MyCLController
@synthesize locationManager;
[B]@synthesize currLoc;[/B]
// Called when the location is updated
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
...
[B] currLoc = [[NSString stringWithFormat:@"%@",newLocation] stringByReplacingOccurrencesOfString: @"@" withString: @","];
// i had to replace the @ in newLocation as this was causing errors elsewhere
NSLog(@"new currLoc: %@",currLoc); // this works fine[/B]
...
}
Code:
/***** File: AppDelegate.m *****/
- (void)applicationDidFinishLaunching:(UIApplication*)application {
...
// starts the gps
[B][[MyCLController sharedInstance].locationManager startUpdatingLocation];[/B]
// log the current location.. [B]this causes a crash![/B]
[B]NSLog(@"%@", [[MyCLController sharedInstance] currLoc];[/B]
...
}