调用[locationManager requestWhenInUseAuthorization]时,警报视图会自行消失. [英] Alert view disappears on its own when calling [locationManager requestWhenInUseAuthorization];

查看:87
本文介绍了调用[locationManager requestWhenInUseAuthorization]时,警报视图会自行消失.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打电话

[locationManager requestWhenInUseAuthorization];

在显示 MKMapView ( showsUserLocation = YES )的屏幕上

.首先看起来很奇怪(Apple应该为 MKMapView 自动处理此问题,但XCode在我不这样做时会抱怨).

on a screen that shows an MKMapView (showsUserLocation = YES). Which seems weird in the first place (Apple should handle this for MKMapView automatically, but XCode was complaining when I didn't do it).

因此,我得到一个警报视图,说应用程序要使用您的位置,但是警报视图会自动消失.

So I get the alert view that says the application wants to use your location, but then the alert view disappears on its own.

为什么警报视图会自行消失?

Why does the alert view disappear on its own?

我只能想到的是,我正在 initWithCoder 方法中调用 requestWhenInUseAuthorization .我之所以这样做,是因为我认为从viewDidLoad调用Xcode时看到了抱怨.

Only thing I can think of is that I am calling requestWhenInUseAuthorization in the initWithCoder method. I'm only doing that because I think I saw the Xcode complaint when calling it from viewDidLoad.

推荐答案

您可能正在使用ARC.确保您仍然对CLLocationManager有引用.您可以通过将其设置为属性来轻松实现此目的.

You're probably being ARC'd. Make sure that you still have a reference to your CLLocationManager. You can easily do this by making it a property.

ARC代表自动引用计数.在一个支持ARC的项目中(除非您正在做的是真正古老的项目,否则您的项目是一个支持ARC的项目),您需要保留对以后将要使用的对象的引用.CLLocationManager不会返回单例,因此您需要在需要的类中保留对其的引用.像这样:

ARC stands for Automatic Reference Counting. In an ARC-enabled project (and unless you are working on something really old or you turned it off on purpose, your project is an ARC-enabled project) you need to keep references to objects that you'll use later on. CLLocationManager doesn't return a singleton so you need to keep a reference to it in your classes that care. Something like this:

@property (strong, nonatomic) CLLocationManager *locationManager

请参阅Apple的 ARC文档有关详细信息.(并感谢Falko努力寻找与之相关的深层链接.)

See Apple's ARC documentation for details. (And thanks Falko for hunting down the deep link to that.)

我把Gobe的评论放在行内,以防您不滚动阅读它.

I'm putting Gobe's comment inline in case you didn't scroll to read it.

对于Swift:不是创建本地范围的locationManager对象,让它作为您关心的课程的属性,例如私人出租locationManager = CLLocationManager(),然后通常将其用作self.locationManager.requestWhenInUseAuthorization()

这篇关于调用[locationManager requestWhenInUseAuthorization]时,警报视图会自行消失.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆