Skip to content

Instantly share code, notes, and snippets.

@hehehaha0228
Created May 12, 2018 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hehehaha0228/e26c044527d5af40c0090f6b319c169f to your computer and use it in GitHub Desktop.
Save hehehaha0228/e26c044527d5af40c0090f6b319c169f to your computer and use it in GitHub Desktop.
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