CLLocationManager didVisit 不工作
CLLocationManager didVisit not working
我创建了一个 class 来处理一些 CoreLocation 内容并在我的 AppDelegate 中对其进行了实例化,但我在开车时没有收到任何 didVisits。我做错了什么?
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let chauffeur = Chauffeur()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Request permission to present notifications
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
return true
}
}
司机
import Foundation
import CoreLocation
import UIKit
import RealmSwift
class Chauffeur : NSObject, CLLocationManagerDelegate {
let clManager = CLLocationManager()
private var viewController: UIViewController?
override init() {
super.init()
clManager.delegate = self
}
func start() {
let status = CLLocationManager.authorizationStatus()
println("status from start: \(status.rawValue)")
// if status != CLAuthorizationStatus.Denied || status != CLAuthorizationStatus.Restricted {
// // make sure we can use location
// if CLLocationManager.locationServicesEnabled() {
// manager.startMonitoringVisits()
// }
// } else if (status == CLAuthorizationStatus.NotDetermined) {
// manager.requestAlwaysAuthorization()
// }
switch status {
case .AuthorizedAlways:
println("monitoring")
clManager.startMonitoringVisits()
case .NotDetermined:
println("request")
clManager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order for the app to work, please open this app's setting page and set location access to 'Always'", preferredStyle: UIAlertControllerStyle.Alert)
let canceAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(canceAction)
let openAction = UIAlertAction(title: "Open Settings", style: UIAlertActionStyle.Default) {
(action) in
if let url = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
viewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
func stop() {
clManager.stopMonitoringVisits()
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
println("Location Status: \(status.rawValue)")
if (status == CLAuthorizationStatus.AuthorizedAlways ||
status == CLAuthorizationStatus.AuthorizedWhenInUse) {
manager.startMonitoringVisits()
}
}
func locationManager(manager: CLLocationManager!, didVisit visit: CLVisit!) {
let realm = Realm()
realm.write {
realm.add(Visit(visit), update: false)
}
if visit.departureDate.isEqualToDate(NSDate.distantFuture() as! NSDate) {
// A visit has begun, but not yet ended. User must still be at the place.
println("Visit begun \(visit)")
showNotification("Visit begun \(visit)")
} else {
// The visit is complete, user has left the place.
println("Visit end \(visit)")
showNotification("Visit end \(visit)")
}
}
func showNotification(body: String) {
let notification = UILocalNotification()
notification.alertAction = nil
notification.alertBody = body
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
func setViewController(viewController: UIViewController) {
self.viewController = viewController
}
}
ViewController
import UIKit
class ViewController: UIViewController {
private var chauffeur: Chauffeur!
override func viewDidLoad() {
super.viewDidLoad()
let ad: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
chauffeur = ad.chauffeur
chauffeur.setViewController(self)
}
@IBAction func toggleTracking(sender: UISwitch) {
if sender.on {
println("on")
chauffeur.start()
} else {
println("off")
chauffeur.stop()
}
}
}
请检查您是否已从项目设置 -> 目标 -> 功能中启用后台模式中的位置。
我创建了一个 class 来处理一些 CoreLocation 内容并在我的 AppDelegate 中对其进行了实例化,但我在开车时没有收到任何 didVisits。我做错了什么?
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let chauffeur = Chauffeur()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Request permission to present notifications
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
return true
}
}
司机
import Foundation
import CoreLocation
import UIKit
import RealmSwift
class Chauffeur : NSObject, CLLocationManagerDelegate {
let clManager = CLLocationManager()
private var viewController: UIViewController?
override init() {
super.init()
clManager.delegate = self
}
func start() {
let status = CLLocationManager.authorizationStatus()
println("status from start: \(status.rawValue)")
// if status != CLAuthorizationStatus.Denied || status != CLAuthorizationStatus.Restricted {
// // make sure we can use location
// if CLLocationManager.locationServicesEnabled() {
// manager.startMonitoringVisits()
// }
// } else if (status == CLAuthorizationStatus.NotDetermined) {
// manager.requestAlwaysAuthorization()
// }
switch status {
case .AuthorizedAlways:
println("monitoring")
clManager.startMonitoringVisits()
case .NotDetermined:
println("request")
clManager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order for the app to work, please open this app's setting page and set location access to 'Always'", preferredStyle: UIAlertControllerStyle.Alert)
let canceAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(canceAction)
let openAction = UIAlertAction(title: "Open Settings", style: UIAlertActionStyle.Default) {
(action) in
if let url = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
viewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
func stop() {
clManager.stopMonitoringVisits()
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
println("Location Status: \(status.rawValue)")
if (status == CLAuthorizationStatus.AuthorizedAlways ||
status == CLAuthorizationStatus.AuthorizedWhenInUse) {
manager.startMonitoringVisits()
}
}
func locationManager(manager: CLLocationManager!, didVisit visit: CLVisit!) {
let realm = Realm()
realm.write {
realm.add(Visit(visit), update: false)
}
if visit.departureDate.isEqualToDate(NSDate.distantFuture() as! NSDate) {
// A visit has begun, but not yet ended. User must still be at the place.
println("Visit begun \(visit)")
showNotification("Visit begun \(visit)")
} else {
// The visit is complete, user has left the place.
println("Visit end \(visit)")
showNotification("Visit end \(visit)")
}
}
func showNotification(body: String) {
let notification = UILocalNotification()
notification.alertAction = nil
notification.alertBody = body
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
func setViewController(viewController: UIViewController) {
self.viewController = viewController
}
}
ViewController
import UIKit
class ViewController: UIViewController {
private var chauffeur: Chauffeur!
override func viewDidLoad() {
super.viewDidLoad()
let ad: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
chauffeur = ad.chauffeur
chauffeur.setViewController(self)
}
@IBAction func toggleTracking(sender: UISwitch) {
if sender.on {
println("on")
chauffeur.start()
} else {
println("off")
chauffeur.stop()
}
}
}
请检查您是否已从项目设置 -> 目标 -> 功能中启用后台模式中的位置。