【问题标题】:Why i create request permission location in framework alert not show long time为什么我在框架警报中创建请求权限位置不显示很长时间
【发布时间】:2017-08-11 22:30:48
【问题描述】:

它显示大约 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() 
    } 
} 

【问题讨论】:

  • 你从哪个线程调用这个方法?
  • 您需要确保您的Location 实例被保留
  • 你能展示用于创建Location的代码吗?
  • import UIKit import DemoLocation class ViewController: UIViewController { deinit { print("deinit") } let location = Location() override func viewDidLoad() { super.viewDidLoad() location.RequestLocation() } }
  • 当类位置的警报权限弹出实例为deinit时

标签: ios swift permissions location core-location


【解决方案1】:

这是因为一旦CLLocationManager 被释放,对话框就会立即消失。

您应该通过实例或静态变量保存对locationManager 的引用

要解决此问题,您必须将 CLLocationManager 更改为存在于类上,而不是当前实例与类一起发布。

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

【讨论】:

  • locationManager 作为实例变量很好,问题似乎是Location 的实例没有被保留在任何地方。
  • 他应该在静态或实例上有一个locationManager的引用
猜你喜欢
  • 2017-05-06
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多