[英]Pass Coordinates from locationManager in appDelegate to viewController
我想在viewController出現時獲取用戶的坐標。 我需要幾個viewControllers中的位置,所以我將locationManager放在appDelegate中。 我的問題是,在第一個viewDidAppear上,尚未找到坐標。 我該怎么做才能改變這個? 謝謝你們!!!
我的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;
}
我的viewController獲取坐標:
- (void)viewDidAppear
{
NSString *userCoordinates =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserCoordinates];
}
我最近在AppDelegate中實現了同樣的東西,位置管理器,以便我的ViewControllers都可以訪問位置數據。
不要忘記實現CoreLocation委托方法,特別是didUpdateLocations以獲取新的位置數據。 我會把它放在AppDelegate類中。
因為,你有一個關系,一個對象(AppDelegate)需要通知許多viewControllers有關位置更新,我建議使用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中,您不會使用viewDidAppear方法。 相反,你會這樣做......
- (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"];
}
您可以讓其他viewControllers通過將它們添加為觀察者來訂閱通知更新。
你還可以做的是將CLLocation
保存為具有緯度和經度的字典到keypath @“currentUserLocation”下的userDefaults(只是一個namingexample)。
在viewDidLoad
您將控制器設置為keypath的觀察者,如下所示:
[[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”),任何觀察者都會得到此信息。 我想它在某種程度上有點像通知中心? :)
這個解決方案適合我的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.