当前位置权限对话框消失得太快了。

184
我的应用程序获取用户位置,获取坐标,并提供到目的地或起点的距离。所有这些可能的目的地都显示在表视图中,因此我在填充表格的同时获取用户坐标。唯一的问题是,询问用户位置的警告视图出现后便立即消失,根本无法单击它!有没有办法在应用程序首次加载时手动显示此警告?我尝试在应用程序加载时获取用户位置以尝试强制显示警报,但这并没有起作用。
12个回答

0

你必须将locationManager变量定义为全局对象。

@interface ViewController : UIViewController
{
    CLLocationManager *locationManager;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];
}

0

我遇到了和你一样的情况。

  • 我的解决方案是将本地变量改为成员实例。
  • 原因是在包含本地变量(扩展我的locationManager)的方法完成后,本地实例无效。
  • 我的环境:Xcode9.3.1
#import 
@interface ViewController ()
@end
@implementation ViewController @synthesize locManager; // after - (void)viewDidLoad { [super viewDidLoad]; // 加载视图后执行任何其他设置,通常是从nib加载。
//MyLocationService *locManager = [[BSNLocationService alloc]init:nil]; // before. the loc. delegate did not work because the instance became invalid after this method. self->locManager= [[MyLocationService alloc]init:nil]; // after locManager.startService; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接