Iphone 将坐标从appDelegate中的locationManager传递到viewController

Iphone 将坐标从appDelegate中的locationManager传递到viewController,iphone,ios,xcode,core-location,locationmanager,Iphone,Ios,Xcode,Core Location,Locationmanager,我试图在viewController出现时获取用户的坐标。我需要几个ViewController中的位置,因此我将locationManager放在appDelegate中。我的问题是,在第一个视图出现时,尚未找到坐标。我该如何着手改变这一点?谢谢大家 我的AppDelegate具有以下功能: - (NSString *)getUserCoordinates { NSString *userCoordinates = [NSString stringWithFormat:@"latitude:

我试图在viewController出现时获取用户的坐标。我需要几个ViewController中的位置,因此我将locationManager放在appDelegate中。我的问题是,在第一个视图出现时,尚未找到坐标。我该如何着手改变这一点?谢谢大家

我的AppDelegate具有以下功能:

- (NSString *)getUserCoordinates
{
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;
}
My viewController通过以下方式获取坐标:

- (void)viewDidAppear 
{
NSString *userCoordinates =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserCoordinates];
}

我最近在AppDelegate中实现了同样的东西,位置管理器,这样我的ViewController都可以访问位置数据

不要忘记实现CoreLocation委托方法,特别是didUpdateLocations,以获取新的位置数据。我会把它放在AppDelegate类中

由于存在一种关系,一个对象(AppDelegate)需要通知多个ViewController有关位置更新,因此我建议使用NSNotification

在AppDelegate中,您可以编写

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// set up location manager
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];
return YES;
}

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations {
CLLocation * newLocation = [locations lastObject];
// post notification that a new location has been found
[[NSNotificationCenter defaultCenter] postNotificationName:@"newLocationNotif"
                                                            object:self
                                                          userInfo:[NSDictionary dictionaryWithObject:newLocation
                                                                                               forKey:@"newLocationResult"]];
}
在ViewController中,您不会使用ViewDidDisplay方法。相反,你会这样做

- (void)viewDidLoad
{
[super viewDidLoad];
// subscribe to location updates
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updatedLocation:)
                                             name:@"newLocationNotif"
                                           object:nil];
}
您将有一个方法updatedLocation,如下所示

-(void) updatedLocation:(NSNotification*)notif {
CLLocation* userLocation = (CLLocation*)[[notif userInfo] valueForKey:@"newLocationResult"];
}

通过将其他ViewController添加为观察者,您还可以让它们订阅通知更新。

您还可以将
CLLocation
保存为字典,并将纬度和经度设置为keypath@“currentUserLocation”下的userDefaults(仅是一个Naming示例)

视图didload
中,将控制器设置为关键路径的观察者,如下所示:

[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"currentUserLocation" options:NSKeyValueObservingOptionNew context:NULL];
并实施类似于:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"currentUserLocation"]) {

    NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
    NSDictionary *dict = [df objectForKey:@"currentUserLocation"];
    NSLog(@"yea we have updated location dict %@", dict);
}
}

这意味着您可以在应用程序中的任何位置更新您的用户位置,只要您更新
UserDefault
(在我的情况下为keyPath@“currentUserLocation”),任何观察者都会得到此信息。我猜它在某种程度上有点像一个通知中心


这个解决方案非常适合我的情况。

效果非常好!非常感谢。但是,用户的初始位置将丢失,因为侦听视图在已发送通知后加载。我在AppDelegate中添加了变量,以保留所有视图访问的位置。@thedp您是如何做到的??您能否显示该变量可从所有视图访问的示例?@UtkuDalmaz我在AppDelegate中放置了一个
CLLocation
变量,并从其他视图控制器使用该
appDelegateInst=(AppDelegate*)[[UIApplication sharedApplication]delegate]。现在您可以访问所有appDelegate变量,如全局变量。