java对话框自动消失,当前位置许可对话框消失得太快

回答(10)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我陷入同样的问题(至少通过症状) . 在我的情况下,问题出在 - (void)applicationWillResignActive:(UIApplication *)application; 方法中,我正在释放我的 CLLocationManager 实例作为准备后台转换的一部分 . 当我删除它并仅在 - (void)applicationDidEnterBackground:(UIApplication *)application; 中将其删除时,问题就消失了 .

棘手的部分是核心位置警报DO在应用程序仍在前台时挂起它 .

希望它会对你有所帮助,花了我很多时间才发现那个混蛋:)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

SWIFT 4 @Zoli解决方案将如下所示:

class WhateverViewController: UIViewController {

let locationManager = CLLocationManager() // here is the point of the @Zoli answer

// some code

override func viewDidLoad() {

super.viewDidLoad()

// some other code

locationManager.requestWhenInUseAuthorization()

// some other code

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我知道这是一个非常晚的回复 . 但它可能会帮助某人 . 我也面临同样的问题,花了一个小时来确定问题 . 起初我的代码是这样的 .

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

[locationManager startUpdatingLocation];

CLLocation *location = locationManager.location;

//my stuff with the location

[locationManager release];

现在,位置警报迅速消失 . 当我取消注释最后一行时,它正常工作 .

// [locationManager release];

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

Swift 4和iOS 11:

确保在 .plist 文件中添加了隐私行(始终和whenUsese)并将 CoreLocation Framework添加到项目中

我改变时正确显示位置权限对话框:

locationManager.requestAlwaysAuthorization()

有:

locationManager.requestWhenInUseAuthorization()

P.S . :我已经尝试了 ALL 建议并且都失败了(请求授权 viewDidLoad , var 而不是 let for locationManager,请勿在请求后启动 startUpdatingLocation() ..我认为这是一个错误,我希望他们能尽快解决它..

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

相同症状,原因不同: do not to call startUpdatingLocation more than once in a row .

我意外地构造了这样的东西,使得代码无意中连续两次调用 startUpdatingLocation ,这显然很糟糕 . 它可能也与队列的选择有关,因为我等待开始更新等待网络请求的结果,但我没有重复启动 .

希望有人能够从我的痛苦中受益 . :)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

虽然难以追踪,但解决方案非常简单 .

通过大量试验和错误,我发现当您第一次尝试访问应用程序中的任何位置服务时弹出位置访问对话框,如果 CLLocationManager 对象被释放,对话框将自行消失(无需任何用户交互)在用户响应对话框之前 .

我在 viewDidLoad 方法中创建了一个 CLLocationManager 实例 . 由于这是该方法的本地实例,因此该方法在完成执行后由ARC释放 . 实例一释放,对话框就消失了 . 解决方案相当简单 . 将 CLLocationManager 实例从方法级变量更改为类级实例变量 . 现在只有在卸载类后才会释放 CLLocationManager 实例 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我也遇到了这个问题,但我的案例中的解决方案与接受的答案完全不同 .

在我的应用程序中,我从 applicationWillResignActive 调用 stopUpdatingLocation . 这是一个问题,因为在出现权限对话框时会调用 applicationWillResignActive . 这在 startUpdatingLocation 之后立即导致 stopUpdatingLocation ,这就是对话框会立即消失的原因 .

解决方案只是从 applicationDidEnterBackground 调用 stopUpdatingLocation .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

您最常将locationManager变量定义为全局对象 .

@interface ViewController : UIViewController

{

CLLocationManager *locationManager;

}

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

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

[locationManager startUpdatingLocation];

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我遇到了你的同样情况 .

我的解决方案已从局部变量更改为成员实例 .

原因是方法完成后本地?实例无效,包括本地变量(扩展我的locationManager)

我的环境:Xcode9.3.1

#import

@interface ViewController ()

@end

@implementation ViewController

@synthesize locManager; // after

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a 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;

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在使用iOS模拟器时,这发生在我身上 . 我确定它发生了,因为我的Run Scheme正在模拟一个位置 . 我认为这与在启动时调用 locationManager.startUpdatingLocation() 具有相同的效果,因此它正在关闭对话框 .

取消选中“编辑方案”对话框中的“允许位置模拟”复选框可修复此问题 . 一旦它按照您的意愿工作并设置了权限,您就可以重新启用位置模拟,从那时起模拟器就能正常工作 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值