appDelegate を使用して、別のビューからユーザーの場所を取得しようとしています。NSUserDefaults に保存することもできましたが、それを行うためのより良い方法があるかどうか疑問に思っていました。
私のAppDelegateにはこれが含まれています:
- (NSString *)deviceLocation
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}
次に、これを使用して別のページからユーザーの場所を呼び出します。
//Get the User's Location
NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];