Reputation: 14113
I have a method in my appDelegate which gets latitude and longitude and returns a string which I can use in any viewController.
AppDelegate :
-(void)StartUpdating
{
locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locManager startUpdatingLocation];
}
#pragma mark
#pragma mark locationManager delegate methods
- (void)locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation: (CLLocation *)oldLocation
{
float latitude = newLocation.coordinate.latitude;
strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
strLongitude = [NSString stringWithFormat:@"%f", longitude];
//[self returnLatLongString:strLatitude:strLongitude];
}
-(NSString*)returnLatLongString
{
NSString *str = @"lat=";
str = [str stringByAppendingString:strLatitude];
str = [str stringByAppendingString:@"&long="];
str = [str stringByAppendingString:strLongitude];
return str;
}
I am calling StartUpdating in start of application. Now in my viewController I call this method:
AppDelegate *appDelegate=[AppDelegate sharedAppDelegate];
NSString *str = [appDelegate returnLatLongString];
But I get a crash in
str = [str stringByAppendingString:strLatitude];
in returnLatLongString
.
I know I am getting crash because there is no value in strLatitude at that time. But how can I fix this? How can I still have updated value of latitude and longitude?
Also, I don't want to use locationManager in viewControllers. So that I can get current location in all viewControllers, I did it in appDelegate.
Upvotes: 1
Views: 4004
Reputation: 127
You are doing a mistake with creating a object of AppDelegate
Class..
To create object just write this code :
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *str = [appDelegate returnLatLongString];
Its done..
Upvotes: 0
Reputation: 6747
There are two potential problems in your code.
First of all make sure you declare strLatitude
and strLongitude
as strong
properties in your header file:
@property (nonatomic, strong) NSString * strLatitude;
@property (nonatomic, strong) NSString * strLongitude;
Use @synthesize
to automatically generate the proper getter and setter for them and assign the proper values to them as:
float latitude = newLocation.coordinate.latitude;
self.strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
self.strLongitude = [NSString stringWithFormat:@"%f", longitude];
to make sure that they don't get released after they are assigned a value, because [NSString stringWithFormat:]
returns autoreleased
objects.
Secondly, after [locManager startUpdatingLocation];
it's going to take sometime before the system delivers the first location update. Therefore you need to check if the strLatitude
and strLongitude
have already been assigned values before you try to construct another string with them. Something similar to this should do the job:
-(NSString*)returnLatLongString
{
if (self.strLatitude == nil || self.strLongitude == nil)
return nil;
NSString *str = @"lat=";
str = [str stringByAppendingString:strLatitude];
str = [str stringByAppendingString:@"&long="];
str = [str stringByAppendingString:strLongitude];
return str;
}
Then of course the view controllers who are going to use [AppDelegate returnLatLongString]
need to check that the returned value is not nil
.
Hope this helps...
Upvotes: 0
Reputation: 201
i think you need to init those variables (strLatitude, strLongitude) in your
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method. if it hits this point at first time, they are not initialized so you get crash.
Upvotes: 0
Reputation:
The crash might be because str
is an NSString, and so therefore is not mutable. Assigning it back to itself in this case is problematic. It would be simpler just to use stringWithFormat
:
NSString *str = [NSString stringWithFormat: @"lat=%@&long=%@", strLatitude, strLongitude];
Upvotes: 5
Reputation: 15213
Import your AppDelegate.h file. Then
Use the following code:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *result = [myAppDelegate returnLatLongString];
Upvotes: 0