我想使用以下代码在我的应用的多个部分中查找用户的位置。为避免复杂性,我只想在一个位置获取用户的位置。我假设我需要将它放在我的 appDelegate 中,然后在我需要使用它时从其他 View 以某种方式调用它。有谁知道我需要使用什么来获取 appDelegate 以外的 View /选项卡上的位置作为 NSString?谢谢你!
- (NSString *)deviceLocation {
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
最佳答案
将所有位置监听器和相关内容移动到应用委托(delegate),然后创建 NSString *location
property AppDelegate
,现在只要你得到一个更新的位置,将其设置为 NSString *location
,在你的其他 ViewControllers
中,当你需要时,访问应用程序代表的位置属性。
如果您确实需要更新位置,可以使用 NSNotificationCenter并广播有关新位置更新的通知,因此真正需要更新位置的 ViewController
将自行注册有关新位置的通知,并相应地执行操作。
关于objective-c - App多处调用位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896906/