I have created a class that handles some of the CoreLocation stuff and instantiated that in my AppDelegate, but I'm not receiving any didVisits as I drive around. What am I doing wrong?
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
}
}
Chauffeur
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()
}
}
}
Please check if you have enable Location in background modes from project settings -> Target -> Capabilities.