gpt4 book ai didi

ios - CLLocationManager didVisit 不工作

转载 作者:行者123 更新时间:2023-11-28 09:28:21 27 4
gpt4 key购买 nike

我已经创建了一个类来处理一些 CoreLocation 内容并在我的 AppDelegate 中对其进行了实例化,但是我在开车时没有收到任何 didVisits。我做错了什么?

应用委托(delegate)

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
}

}

View Controller

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()
}
}

}

最佳答案

请检查您是否已从项目设置 -> 目标 -> 功能中启用后台模式中的位置。

enter image description here

关于ios - CLLocationManager didVisit 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com