CLLocationManager didVisit 不工作

Posted

技术标签:

【中文标题】CLLocationManager didVisit 不工作【英文标题】:CLLocationManager didVisit not working 【发布时间】:2015-06-10 04:51:09 【问题描述】:

我创建了一个类来处理一些 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
    


视图控制器

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


【问题讨论】:

您是否在后台模式下启用位置更新 定位服务始终开启 哦,我没有启用后台模式 - 功能中的位置更新。 【参考方案1】:

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

【讨论】:

我今天试过了,但我的领域数据库中仍然没有收到任何通知或任何写入。我的代码还有什么问题吗? 遇到同样的问题 - 没有得到任何访问。 我在一个被定义为“访问”的区域内并启动应用程序,启用此 bg 模式,开始监视访问但没有得到任何东西... 不要那样做。对于非常特定的用例,需要将位置更新作为后台模式。您不需要它来访问。

以上是关于CLLocationManager didVisit 不工作的主要内容,如果未能解决你的问题,请参考以下文章

为啥'CLLocationManager.locationServicesEnabled()'默认为真?

Xcode 6 GM - CLLocationManager

CLLocationManager 和 CLGeoCoder

CLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons

CLLocationManager 最后一个已知位置

CLLocationManager 从不调用委托方法[重复]