appDelegate に、緯度と経度を取得し、任意の 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;
}
アプリケーションの開始時にStartUpdatingを呼び出しています。今私のviewControllerで私はこのメソッドを呼び出します:
AppDelegate *appDelegate=[AppDelegate sharedAppDelegate];
NSString *str = [appDelegate returnLatLongString];
しかし、私はクラッシュします
str = [str stringByAppendingString:strLatitude];
でreturnLatLongString
。
その時点でstrLatitudeに値がないため、クラッシュしていることはわかっています。しかし、どうすればこれを修正できますか? 緯度と経度の値を更新するにはどうすればよいですか?
また、 viewControllers でlocationManagerを使用したくありません。すべてのviewControllerで現在の場所を取得できるように、appDelegateで行いました。