Skip to content

Instantly share code, notes, and snippets.

@hehehaha0228
Created May 12, 2018 14:09
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/8ae4078872ea39708c169447e1d77eef to your computer and use it in GitHub Desktop.
Save hehehaha0228/8ae4078872ea39708c169447e1d77eef to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// myNavegationLayoutAndToolBar
//
// Created by Chang Shu Hao on 2018/5/5.
// Copyright © 2018年 Gary. All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var myLocationManager : CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
myLocationManager.delegate = self
myLocationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.authorizationStatus() == .denied {
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()
}//else if
}//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