Skip to content

Instantly share code, notes, and snippets.

@hehehaha0228
Last active June 7, 2018 06:05
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/fd56024a56ba2d01fab48f2ed8a75cbf to your computer and use it in GitHub Desktop.
Save hehehaha0228/fd56024a56ba2d01fab48f2ed8a75cbf to your computer and use it in GitHub Desktop.
import UIKit
import CoreLocation
import MapKit
//記得要import CoreLocation跟Mapkit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var myLocationManager : CLLocationManager = CLLocationManager()
var myUserDefaults : UserDefaults!
let userLocationAuth : String = "locationAuth"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.myUserDefaults = UserDefaults.standard
myLocationManager.delegate = self
myLocationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest
myLocationManager.activityType = .automotiveNavigation
myLocationManager.startUpdatingHeading()
if CLLocationManager.authorizationStatus() == .notDetermined{
myLocationManager.requestWhenInUseAuthorization()
}//if
else if CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .restricted {
self.myUserDefaults.set(false, forKey: userLocationAuth)
self.myUserDefaults.synchronize()
//這邊可以設定你想要執行的功能,例如這邊我想要紀錄使用者的權限狀況,所以用了userDefaults的功能。
//如果你沒有想要執行任何功能,else if 這一段這一段可以完全不寫。
}//else if
else if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
self.myUserDefaults.set(true, forKey: userLocationAuth)
self.myUserDefaults.synchronize()
//這一段同上,如果你沒有想要執行任何功能,這一段可以不寫
//
}//else if
return true
}//didFinishLaunchingWithOptions
}//class
//在大引號後面加上它來自哪個開頭是我的習慣,不然我很容易找不到而森77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment