簡體   English   中英

iOS 8和Swift中的MapKit

[英]MapKit in iOS 8 and Swift

我試圖在iOS 8上使用MapKit,但不斷收到錯誤消息:

Trying to start MapKit location updates without prompting for location authorization. Must call   
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager 
requestAlwaysAuthorization] first. 

在這里查找時,我發現我必須在我的plist中實現NSLocationWhenInUsageDescription ,還必須調用locationManager.requestWhenInUseAuthorization()但是什么也沒有發生,並且我仍然在控制台中收到該錯誤。 我究竟做錯了什么?

在我的應用程序委托中,我為類之外的locationManager創建了一個可選的var,然后進行設置

locManager = CLLocationManager()
locManager!.requestWhenInUseAuthorization()

如果您進行適當的更改,這將使警報視圖隨您的NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription一起彈出。

然后,在視圖控制器文件中,我在類外創建了另一個var,以保存本地CLLocationManager。 然后我設置

if locManager {
        locMan = locManager!
        locMan!.delegate = self
    }

然后可以使用委托方法

func locationManager(_manager: CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus)

授權狀態更改時將調用該方法,當用戶響應彈出窗口時將執行該操作。 在其中,您可以使用以下代碼在地圖上放置用戶位置

if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        map.showsUserLocation = true
    }

僅在使用時授權您,才將用戶位置添加到地圖。

我也被那個困擾,直到我意識到info.plist鍵已經改變。 如果您有NSLocationUsageDescription ,則需要更改為NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription (至少將其設置為YES ),現在在iOS8中是必需的。

然后,Robert的代碼可以正常工作(感謝共享)。

它是NSLocationWhenInUseUsageDescription,而不是NSLocationWhenInUsageDescription。 網上大多數地方輸入的密碼都不正確

暫無
暫無

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

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