如何在 iOS 應用中檢查是否啟用了定位服務?
顧名思義,定位服務透過 GPS、Wifi 和蜂窩訊號塔收集使用者資訊。每部 iOS 裝置都配有 GPS、WiFi、蜂窩定位資料和藍牙,以確定 iPhone 或 iPad 的位置。使用者可以透過輕觸“常規”中的“定位服務”開關在“設定”應用中啟用或停用定位服務。
在開始定位更新之前,你應檢查 locationServiceEnabled() 方法的返回值,以確定使用者是否為當前裝置啟用了定位服務。
要檢查定位服務是否在 iOS 應用中啟用,請檢出程式碼
開啟 Xcode → 在 ViewController 中新建專案,建立如下新函式,你就大功告成了。
func isLocationAccessEnabled() { if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .notDetermined, .restricted, .denied: print("No access") case .authorizedAlways, .authorizedWhenInUse: print("Access") } } else { print("Location services not enabled") } }
廣告