点击“想要使用您当前位置”消息的“确定”后,首次运行时 GPS 不会启动。我有一个表格 View 来显示当前位置周围的地点名称。表中没有 map View ,仅采用CoreLocation框架。当我第一次构建并运行我的应用程序并进入表格 View 时,会跳出著名的消息““myApp”想要使用您当前的位置”。单击“确定”按钮后,GPS 不再运行。但当我再次进入表格 View 时,GPS 运行良好。
问题是:点击“确定”按钮后,如何让 GPS 在第一次启动时运行?提前致谢!
编辑1
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
是否可以检测到“想要使用您当前的位置”消息的“确定”按钮被点击?
最佳答案
向您展示一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
}
- (void)timerFired:(NSTimer*)theTimer{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
[self startUpdatingCurrentLocation];
[theTimer invalidate];
}
[self startUpdatingCurrentLocation];
}
计时器会等待一定的时间间隔,然后触发,向目标对象发送指定的消息。 NSTimer Reference.
如果用户按下“确定”按钮,计时器可以以透明的方式第二次启动 GPS。如果没有,计时器将继续检查 GPS 状态,直到应用程序获得用户的选择。
关于iphone - 单击 "OK"后首次运行时 GPS 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10872491/