簡體   English   中英

如何在iOS 8或更舊版本上實現CLLocationManager.requestLocation()?

[英]How to implement CLLocationManager.requestLocation() on iOS 8 or older?

如果您已經閱讀了新的iOS 9文檔,則可能已經注意到一種新方法,該方法可使位置管理器在短時間內打開收音機的電源,修復您的位置,並在達到所需的准確性(或超時)后,將其全部關閉,將精確的位置移交給代表。

該方法稱為CLLocationManager.requestLocation() ,在iOS 9 SDK中可用。 las,我目前正在開發針對iOS 8的應用,仍然非常想使用此方法。 我也不想自己全部重新實現它。

所以這是我的問題:是否有用於此類一次性位置檢索的iOS開源庫?

您首先需要執行兩個步驟

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

然后

var locationManager = CLLocationManager()
    locationManager.delegate = self
    // locationManager.locationServicesEnabled
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    let Device = UIDevice.currentDevice()

    let iosVersion = NSString(string: Device.systemVersion).doubleValue

    let iOS8 = iosVersion >= 8

    if iOS8{

        locationManager.requestWhenInUseAuthorization()
    }
else{
locationManager.requestAlwaysAuthorization()
}

locationManager.startUpdatingLocation()

這會有所幫助。謝謝

正如Arslan Asim指出的那樣,一個好的解決方案是使用INTULocationManager

從文檔:

manager.requestLocationWithDesiredAccuracy(.Block, timeout: 10.0) {
    (currentLocation, achievedAccuracy, status) in
    // Examine status to see if the request timed out.
    // If not, currentLocation stores the found location.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM