使用locationManager委托(“作弊方式")SWIFT在后台模式下运行NSTimer [英] NSTimer in background mode using locationManager delegate ("Cheat way") SWIFT

查看:200
本文介绍了使用locationManager委托(“作弊方式")SWIFT在后台模式下运行NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,希望在应用程序后台模式下运行,并至少每分钟执行一次,以检查wifi连接并做出决定.我正在使用 locationManager委托,因此可以在后台运行 NSTimer .但是,位置管理器正在消耗大量电池电量.此应用程序不适用于苹果发行.但是,我正在寻找位置管理器的更有效的设置,这样就不会因为耗电或其他任何好主意? 我目前的设置还可以,但是由于我启用了位置管理器的自动暂停功能,因此功能更新被延迟太多了.在使用两个委托方法(didEnterRegion和didExitRegion)之前,这些方法比较耗电且不准确.我阅读了大量可用的教程,并检查了Stack Overflow上的其他相关文章,但没有找到任何可以帮助我解决问题的内容 这是我的委托函数中的内容:

I have a function that I want to run in the app background mode and be executed at least every minute to check for wifi connection and to make decisions. I am using locationManager delegate so I can run my NSTimer in the background. However the location manager is consuming a lot of battery power. This app is not for apple release. However I am looking for more efficient settings for location manager so it will not be that power hungry or maybe any other good ideas?. The current settings that I have are ok, but since I enabled automatic pause for location manager, function updates are delayed too much. Before I was using two delegates methods (didEnterRegion and didExitRegion) those were more power-hungry and not accurate. I read tons of available tutorials and checked other related posts on Stack overflow but have not found anything that would help me to solve my problm Here is what I have in my delegate function:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.timer = NSTimer.scheduledTimerWithTimeInterval(45, target: self, selector: #selector(self.checkNetworkSSID), userInfo: nil, repeats: true)
        manager.stopMonitoringSignificantLocationChanges()
        manager.stopUpdatingLocation()
    }

这是我的 viewDidLoad AppDelegate

manager = CLLocationManager()
        manager?.delegate = self
        manager?.requestWhenInUseAuthorization()
        manager?.startUpdatingLocation()
        manager?.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        manager?.pausesLocationUpdatesAutomatically = true
        manager?.activityType = CLActivityType.Fitness

推荐答案

要在后台获取位置,您需要在自己的计算机上调用此代码 代码,并确保在您的info.plist

For getting Location in Background you need to call this code on your code and make sure add NSLocationAlwaysUsageDescription inside your info.plist

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.startMonitoringSignificantLocationChanges()
        if #available(iOS 9.0, *) {
            locationManager.allowsBackgroundLocationUpdates = true
        }
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

这篇关于使用locationManager委托(“作弊方式")SWIFT在后台模式下运行NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆