Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
import CoreLocation
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate {
var myLocationManager : CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
myLocationManager.delegate = self
myLocationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.authorizationStatus() == .denied {
self.myUserDefaults.set(false, forKey: userLocationAuth)
self.myUserDefaults.synchronize()
DispatchQueue.main.async(){
let alertController = UIAlertController(title: "定位權限已關閉", message: "如要變更權限,請至 設定 > 隱私權 > 定位服務 開啟", preferredStyle: .alert)
let okAction = UIAlertAction(title: "ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}//DispatchQueue
}
}//ViewDidLoad
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
myLocationManager.startUpdatingLocation()
}//ViewDidAppear
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
myLocationManager.stopUpdatingLocation()
}//viewDidDisappear
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}//didReceiveMemoryWarning
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.denied{
let alertController = UIAlertController(title: "定位權限已關閉", message: "請至 設定 > 隱私權 > 定位服務 開啟定位功能", preferredStyle: .alert)
let okAction = UIAlertAction(title: "ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}//if
else if status == CLAuthorizationStatus.authorizedWhenInUse{
myLocationManager.startUpdatingLocation()
}//else if
}//func didChangeAuthorization
}//class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment