ios - 位置服务已启用 : APIs deprecated

标签 ios swift

我使用来自 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 中不可用

谁知道怎么解决?

谢谢!

最佳答案

CLLocationManager实例 上的locationServicesEnabled 属性is deprecated从 iOS 4.0 开始,但是 class method 不是。

所以代替:

locationManager.locationServicesEnabled

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

CLLocationManager.locationServicesEnabled()

关于ios - 位置服务已启用 : APIs deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26319156/

相关文章:

swift - 将带有参数的闭包传递给 SwiftUI View

ios - 如何在 View 中移动 UIPageViewController 的 PageController 按钮位置

ios - Swift 中的协议(protocol)声明 - Xcode 6 beta 5

xcode - XCode/Swift 中缺少类

swift - 如何阅读此 Swift 语法?

ios - titleLabel 无法在 UIButton 中正确适应高度

带有双点的 Swift 非包含范围

ios - Objective-C 中的属性

ios - UITableViewCell 中 UIStackView 中的 UICollectionview

iOS动画 block : ignores animation commands