【问题标题】:Swift - Location Prompt not happening soon enoughSwift - 位置提示没有很快发生
【发布时间】:2017-03-22 15:44:19
【问题描述】:

我正在构建一个基于位置的应用程序,它会列出附近的咖啡馆。应用程序在设备上首次构建时不断崩溃,因为位置一直返回为零。

这是因为隐私 - 位置提示没有很快发生,即使该请求在代码中较早。在应用程序崩溃后关闭应用程序后,系统会提示我允许我的位置。

我有三个引导屏幕,当我到达这个 tableviewcontroller 时,它就崩溃了。

如果我进入“设置”>“隐私”>“位置”并手动启用位置服务,该应用运行良好。

这是我的代码(我删除了大量不必要的东西):

import UIKit
import MapKit
import CoreLocation

class ShopTableViewController: UITableViewController, CLLocationManagerDelegate {




@IBAction func filterBack(_ sender: Any) {
        getLocale()
        shops.sort() { $0.distance < $1.distance }
        shops.removeAll()
        loadShops()
        sortList()

}


//MARK: Properties

var shops = [CoffeeShop]()
var filteredShops = [CoffeeShop]()
var objects: [CoffeeShop] = []
var locationManager = CLLocationManager()
func checkLocationAuthorizationStatus() {
    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        locationManager.requestWhenInUseAuthorization()
    }
}
var currentLocation = CLLocation!.self
var userLatitude:CLLocationDegrees! = 0
var userLongitude:CLLocationDegrees! = 0
var locValue:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 1.0, longitude: 1.0)
var refresher: UIRefreshControl! = UIRefreshControl()





func getLocale() {

    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    self.locationManager.startMonitoringSignificantLocationChanges()

    userLatitude  = self.locationManager.location?.coordinate.latitude
    userLongitude = self.locationManager.location?.coordinate.longitude
    print("\(userLatitude), \(userLongitude)")
}



override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager = CLLocationManager()
    /// self.locationManager.requestWhenInUseAuthorization()
    checkLocationAuthorizationStatus()

    self.locationManager.delegate = self
    self.locationManager.startUpdatingLocation()
    if CLLocationManager.locationServicesEnabled()
    {
        getLocale()

    }

    let locValue = self.locationManager.location?.coordinate
    noHeight()
    loadShops()
    sortList()
    print("\(locValue?.latitude), \(locValue?.longitude)")

    refresher = UIRefreshControl()
    refresher.addTarget(self, action: #selector(ShopTableViewController.handleRefresh), for: UIControlEvents.valueChanged)


    if #available(iOS 10, *) {
        shopTable.refreshControl = refresher
    } else {
        shopTable.addSubview(refresher)
    }


}
}

我做错了什么?

【问题讨论】:

  • checkLocationAuthorizationStatus 中的条件是否错误?例如应该是CLLocationManager.authorizationStatus() != .authorizedWhenInUse

标签: ios swift core-location locationmanager


【解决方案1】:

requestWhenInUseAuthorization() 是一个异步方法,所以你包装它的方法checkLocationAuthorizationStatus() 也是异步的。

但是,在您的viewDidLoad 中,您调用

checkLocationAuthorizationStatus()

self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()

这会触发 locationManager 在授权之前启动。看看这里这个(有点旧的)链接http://nshipster.com/core-location-in-ios-8/

示例

一定要符合CLLocationManagerDelegate

override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager = CLLocationManager()
    self.locationManager.delegate = self
    if CLLocationManager.authorizationStatus() == .notDetermined {
        locationManager.requestWhenInUseAuthorization()
    } else if CLLocationManager.authorizationStatus() == . authorizedWhenInUse {
        startTrackingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways || status == .authorizedWhenInUse {
        startTrackingLocation()
        // ...
    }
}

func startTrackingLocation() {
    locationManager.startUpdatingLocation()
    getLocale()
    //not clear which of these methods require location
    let locValue = self.locationManager.location?.coordinate
    noHeight()
    loadShops()
    sortList()
    print("\(locValue?.latitude), \(locValue?.longitude)")
}

【讨论】:

  • 所以我需要移动一些东西?我很难将那篇文章翻译成我的代码。对不起,新手
  • 谢谢!我现在必须移动一些其他的东西,以确保表格在位置跟踪开始后更新它的数据,这完全缓解了我的头痛!谢谢!
【解决方案2】:

您需要等待授权响应才能使用定位服务。 您现在正在做的是请求授权并立即启动位置服务。在获取位置之前,您需要确保该应用已获得授权。

【讨论】:

  • 这可以通过移动(重组)来完成吗?
  • @scttcrry 基本上是的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多