为什么我在框架警报中创建请求权限位置不显示很长时间

时间:2017-03-20 15:04:46

标签: ios swift permissions location core-location

显示约3秒并解雇

但是当我在正常课程中创建它显示所有时间然后用户点击以回答允许/不允许

public class Location: NSObject, CLLocationManagerDelegate{
    public var locationManager = CLLocationManager()

    public func RequestLocation(){
        self.locationManager.delegate = self
        if CLLocationManager.authorizationStatus() == .notDetermined {
            self.locationManager.requestWhenInUseAuthorization()
        }

        locationManager.startUpdatingLocation()
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
}

编辑 - 添加了OP代码以显示Location实例化

import UIKit 
import DemoLocation 
class ViewController: UIViewController { 
    deinit { 
        print("deinit") 
    } 

    let location = Location() 
    override func viewDidLoad() { 
        super.viewDidLoad() 
        location.RequestLocation() 
    } 
} 

1 个答案:

答案 0 :(得分:0)

这是因为只要释放CLLocationManager,对话框就会立即消失。

您应该通过实例或静态变量

保存对locationManager的引用

要解决此问题,您必须更改类中存在的CLLocationManager与要与该类一起发布的当前实例。

有关CLLocationManager的更多帮助,请参阅Apple API Reference