CLLocationManager-iPhone应用程序中的自定义位置管理器

问题描述 投票:0回答:2

我对这项技术非常陌生,请帮助我。

我正在使用CLLocationManager来获取当前位置,并且我的应用程序具有自定义设置,显示了他是否允许当前位置的状态,即,在编写时默认出现的消息提示

[self.locationManager startUpdatingLocation];

如果用户点击不允许在该消息提示上,然后返回到我的应用程序自定义设置,并更改了“关闭”按钮以打开位置,我可以获取用户的当前位置。

如何实现?

iphone ios location cllocationmanager
2个回答
15
投票

一旦您的应用被用户拒绝了位置服务,则只能在常规设置中将其重新打开-因此请告知用户,然后将其发送到该位置。 (您可能想使用UIAlertView首先通知它们。)

// If Location Services are disabled, restricted or denied.
if ((![CLLocationManager locationServicesEnabled])
    || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
    || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
{
    // Send the user to the location settings preferences
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]];
}

0
投票

您的问题的简短答案是“是”。当用户通过设置为您的应用启用位置服务时,就像用户在系统提示时回答“是”一样。您可以通过检查代码中的授权状态来检查是否为您的应用启用了位置服务,因此:

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) 
{
    //Get user's current location


}
© www.soinside.com 2019 - 2024. All rights reserved.