LocationServicesEnabled:API 已弃用

Posted

技术标签:

【中文标题】LocationServicesEnabled:API 已弃用【英文标题】:LocationServicesEnabled: APIs deprecated 【发布时间】:2014-10-11 20:50:34 【问题描述】:

我使用来自here的代码:

import UIKit 
import CoreLocation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate 

var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool 
    initLocationManager();
    return true


// Location Manager helper stuff
func initLocationManager() 
    seenError = false
    locationFixAchieved = false
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.locationServicesEnabled
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestAlwaysAuthorization()


// Location Manager Delegate stuff

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)")
        


但我有一个错误: 'locationServicesEnabled' 不可用:从 iOS 7 起弃用的 API 在 Swift 中不可用

谁知道,怎么解决?

谢谢!

【问题讨论】:

【参考方案1】:

ios 4.0 起,CLLocationManageris deprecated 的实例上的 locationServicesEnabled 属性,但 class method 不是。

所以而不是:

locationManager.locationServicesEnabled

您应该简单地使用以下内容:

CLLocationManager.locationServicesEnabled()

【讨论】:

自 iOS 4.0 起已弃用实例方法 @smileyborg 谢谢!抱歉,我忘记了 AnyObject[]! 的错误。我必须使用[AnyObject]!吗? @tim 该委托方法的最新 Swift 语法是:locationManager(_ manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) @smileyborg 我有一个错误:App -[CLLocationManager requestAlwaysAuthorization]: unrecognized selector sent to instance 0x7b6d1550 App *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[CLLocationManager requestAlwaysAuthorization] : 无法识别的选择器发送到实例 0x7b6d1550' @tim 这些是仅在 iOS 8.0 或更高版本中可用的新方法。如果您在 iOS 7 或更早版本上运行,您将遇到这些崩溃。如果您尝试同时支持这两种方法,您可能需要考虑使用像 INTULocationManager 这样的库来为您处理所有工作。

以上是关于LocationServicesEnabled:API 已弃用的主要内容,如果未能解决你的问题,请参考以下文章

CLLocationManager locationServicesEnabled 开启但个别关闭

locationServicesEnabled 测试在 viewDidLoad 中被禁用时通过

检查 locationServicesEnabled 始终返回 YES,无论手动拨动开关决定是不是启用了位置服务

ios 打开app应用权限

定位权限授权 - iOS

Map定位