27 cllocationmanager swift ios8
我一直试图让它工作一段时间,我来这里问 - 如何在Swift中使用CLLocationManagerDelegate方法?我把它放在班上的顶端:
var locationManager = CLLocationManager()
我把以下内容放入我的viewDidLoad方法中:
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
我尝试使用这些委托方法无济于事:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
    locationReceived = true
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationReceived = false
}
我也尝试在函数前使用@optional,但Xcode会抛出编译器错误.有任何想法吗?
Del*_*D0D 58
你需要在你的plist 上添加NSLocationAlwaysUsageDescription或者NSLocationWhenInUseUsageDescription键,如果你还没有,它们现在是强制性的,
iOS8 +要求将这两个字符串中的一个设置为使用位置.您使用哪一个取决于您打算如何询问该位置.
使用NSLocationAlwaysUsageDescription对于要使用的设备的位置,即使应用没有打开并正在使用的应用程序.
仅NSLocationWhenInUseUsageDescription适用于想要在应用程序打开和使用时使用设备位置的应用程序.
注意:在构建和运行之前添加字符串时,请从设备中删除该应用程序并让其进行全新安装.似乎如果应用程序在升级到iOS8之前被授权使用位置,则它不会再次请求您的许可,也不会看到您设置这些字符串.执行删除和清理安装解决了这个问题.
设置任一字符串会在安装/首次使用时提示弹出:"允许"ThisApp"访问您的位置,即使您不使用该应用程序"
这里有一个截图中的的plist文件.
首先在plist文件中添加这两行
1)NSLocationWhenInUseUsageDescription
2)NSLocationAlwaysUsageDescription
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
override func viewDidLoad() {
    super.viewDidLoad()
}
  func initLocationManager() {
   seenError = false
   locationFixAchieved = false
    locationManager = CLLocationManager()
   locationManager.delegate = self
   locationManager.locationServicesEnabled
   locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
}
  func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationManager.stopUpdatingLocation()
    if (error) {
        if (seenError == false) {
            seenError = true
           print(error)
        }
    }
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as CLLocation
        var coord = locationObj.coordinate
        println(coord.latitude)
        println(coord.longitude)
    }
}
 func locationManager(manager: CLLocationManager!,
    didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        var shouldIAllow = false
        switch status {
        case CLAuthorizationStatus.Restricted:
            locationStatus = "Restricted Access to location"
        case CLAuthorizationStatus.Denied:
            locationStatus = "User denied access to location"
        case CLAuthorizationStatus.NotDetermined:
            locationStatus = "Status not determined"
        default:
            locationStatus = "Allowed to location Access"
            shouldIAllow = true
        }
        NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
        if (shouldIAllow == true) {
            NSLog("Location to Allowed")
            // Start location services
            locationManager.startUpdatingLocation()
        } else {
            NSLog("Denied access: \(locationStatus)")
        }
}
获取用户当前位置:-
步骤1: let locationManager = CLLocationManager() // make object of CLLocationManager class.
第2步: In viewDidLoad instantiate the CLLocationManager class like,
// 用于后台
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
第3步:现在实现委托方法CLLocationManager
  func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var locValue:CLLocationCoordinate2D = manager.location.coordinate
    println("locations = \(locValue.latitude) \(locValue.longitude)")
  }
第 4 步:
      不要忘记在 Info.plist 中添加NSLocationAlwaysUsageDescription,因为在 iOS 8 中必须添加此内容。这将请求使用用户位置的许可。
| 归档时间: | 
 | 
| 查看次数: | 40061 次 | 
| 最近记录: |