CLLocationManager未请求用户的权限

时间:2015-05-11 10:50:39

标签: ios swift cllocationmanager

我正在使用Swift中的应用程序,它必须获取用户的GPS坐标。我已将密钥(NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription)添加到info.pList,如下所示:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Location is required to find your coordinates</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location is required to find your coordinates</string>

我还在viewDidLoad方法中添加了CoreLocation.framework和请求的授权:

    override func viewDidLoad() {
    super.viewDidLoad()

    var locManager = CLLocationManager()

    locManager.requestWhenInUseAuthorization()

    locManager.startUpdatingLocation()

    if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){

            var currentLocation = locManager.location
            longitude = currentLocation.coordinate.longitude
            latitude = currentLocation.coordinate.latitude
            textBox.text = longitude.description + latitude.description

    }
}

正如标题中所述,问题是未提示用户提供权限,因此该位置永远不会更新。

2 个答案:

答案 0 :(得分:0)

首先添加此行

locManager.delegate = self;

override func viewDidLoad() {
super.viewDidLoad()

locManager.delegate = self;
locManager.requestWhenInUseAuthorization()

locManager.startUpdatingLocation()

if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){

        var currentLocation = locManager.location
        longitude = currentLocation.coordinate.longitude
        latitude = currentLocation.coordinate.latitude
        textBox.text = longitude.description + latitude.description

}

比在你的类中添加CLLocationManagerDelegate 示例

  class ViewController: UIViewController, CLLocationManagerDelegate

最后改变了

的范围
var locManager = CLLocationManager() 

例如

class ViewController: UIViewController ,CLLocationManagerDelegate{
  var locManager = CLLocationManager()
  override func viewDidLoad() {
  ...

答案 1 :(得分:0)

将locManager作为ViewController的属性,而不是局部变量。

    QXmlStreamReader xs(ba);
    int i=-2;
    QTableWidgetItem* titem=NULL;

    QStringList qslHeaders;
    map < QString, pair<QString, QString >> HeaderMap;
    int map_index = 0;
    int col_count_set = 0;
    map<QString, pair<QString, QString>>::iterator iter;
    while (!xs.atEnd())
    {
        bool flag = true;
        ++i;
        if (xs.readNextStartElement() && xs.name().toString() == "ROW")
        {
            insertRow(i);
            while (flag)
            {
                switch (xs.readNext())
                {
                case QXmlStreamReader::StartElement:
                {
                    QString name = xs.name().toString();

                    //Insert data to map here
                }
                break;

                case QXmlStreamReader::EndElement:
                    if (xs.name().toString() == "ROW")
                    {
                        map_index = 0;
                        if (HeaderMap.size()>0 && col_count_set == 0)
                        {
                            setColumnCount(HeaderMap.size());
                            col_count_set++;
                        }
                        iter = HeaderMap.begin();
                        while (iter != HeaderMap.end())
                        {
                            titem = new QTableWidgetItem(iter->second.first);
                            setItem(i, map_index, titem);
                            iter++;
                            map_index++;
                        }
                        flag = false;
                    }
                    break;
                default:
                    break;
                }
            }
        }
        // WRITTEN FOR DEBUGGING
        if (i > 501)
            break;
       // DEBUG
    }
    iter = HeaderMap.begin();
    while (iter != HeaderMap.end())
    {
        if (!qslHeaders.contains(iter->second.second))
        {
            qslHeaders.append(iter->second.second);
        }
        iter++;
    }
    setHorizontalHeaderLabels(qslHeaders);