[英]Location Services in IOS, prompt
我的应用程序需要位置服务才能正常工作,因此首次运行该应用程序时会向用户询问问题。 如果用户说“ OK”,那么它将继续运行整个应用程序而不会继续运行,因为它将继续执行其工作。 因此,第一次运行并不会真正给视图控制器带来任何好处。
当我第二次按XCODE中的“执行”按钮时,它可以正常工作,因为用户已经授权使用位置服务来获取经度/纬度。
有人遇到过这个问题吗? 有什么建议吗?
在您的viewDidLoad:
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
您需要做的是在“ didUpdateLocation”调用之后更新视图,然后调用“ SetNeedsDisplay”,这样您的视图便会绘制自身以进行更新。 像这样:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
self.LabellLatitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude];
self.LabelLongitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude];
[self.view setNeedsDisplay];
}
如果您添加一个活动指示器直到位置更新,那就更好了。
现在,如果您从“ AppDelegate”类中调用此方法,则可以使用“ NSNotificationCenter”将消息发送到视图,更新标签或视图,或任何您需要的对象,例如:
#pragma mark - locationManager:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdatedLocation" object:nil userInfo:dictionary];
}
然后在您的UIVIewController ViewDidLoad上,使用此用户注册通知:
//Adding self to notification Center:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"UpdatedLocation" object:nil];
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.