【问题标题】:How to Disable/Enable location in swift如何快速禁用/启用位置
【发布时间】:2016-05-23 13:21:01
【问题描述】:

我想通过点击一个按钮来启用/禁用用户位置。

在我的 AppDelegate 中有这个:

    let core = CLLocationManager()

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            core.requestWhenInUseAuthorization()
             ...

在我的 OptionController 我有这个:

 @IBAction func disablePermissionLocation(sender: AnyObject) {
    }

但我不知道如果用户在首次启动应用时禁用了位置信息,或者如果用户在首次启动应用程序时启用了位置信息,我该如何启用它。

【问题讨论】:

    标签: ios swift cllocationmanager


    【解决方案1】:

    如果你有两个按钮而不是这样:- 添加委托 - CLLocationManagerDelegate

    //global varible :
    var  locationManager = CLLocationManager()
    func viewdidLoad(){
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
      }
    func enableLocationManager() {
        locationManager.startUpdatingLocation()
    }
    
    func disableLocationManager() {
        locationManager.stopUpdatingLocation()
    }
    

    【讨论】:

    • 提示:要使用 startUpdatingLocation(),您必须在项目功能中启用位置更新后台模式 警告:使用 startUpdatingLocation() 将导致实时位置更新,Apple 对此非常重视,将后台模式用于任何其他目的,然后位置更新将导致应用被拒绝:)
    • 我问问题接受答案哥们:)问错人了:)
    • 感谢您的回答 Chaudhary 但我想禁用位置授权而不是地图中跟踪模式的更新。
    • 你是说禁用位置授权警报,要求用户允许应用程序访问定位服务或其他东西
    • 例如:第一次启动应用程序时,会出现要求用户允许访问位置的警报并且您接受。一天后你想通过点击按钮来禁用它允许这个的命令是什么?
    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多