热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在后台计数(NSTimer超过3分钟)-Countwhileinbackground(NSTimerformorethan3mins)

IsthereanywaytorunNSTimerformorethan3minsinbackground?Ihavetocreatesimpleappthat

Is there any way to run NSTimer for more than 3 mins in background? I have to create simple app that uses

有没有办法在后台运行NSTimer超过3分钟?我必须创建使用的简单应用程序

 `locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion)`

for scan my beacons. I have run into problem when I needed to check if user is exactly 10 seconds near closest beacon. I created

扫描我的信标。当我需要检查用户是否距离最近的信标10秒时,我遇到了问题。我建立

    var timer: NSTimer?
    var lastClosestBeacon: CLBeacon? {
        didSet {
            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "showLocalNotification", userInfo: nil, repeats: false)
//            NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSDefaultRunLoopMode)
        }
    }

When closest beacon changes lastClosestBeacon is set and timer is scheduled. It is working in App and for 3 mins when user enters background(locks phone etc.) with help from Scheduled NSTimer when app is in background? Is there any possibility to check that for more than 3 mins ?

当最近的信标更改时,设置lastClosestBeacon并安排计时器。当应用程序处于后台时,当用户在预定NSTimer的帮助下进入后台(锁定手机等)时,它正在App中工作3分钟?有没有可能检查超过3分钟?

PS. I have already added Background Modes with location updates.

PS。我已经添加了带有位置更新的后台模式。

2 个解决方案

#1


4  

You can do it by following way:

您可以通过以下方式完成:

1) First include required background mode keys into your Info.plist

1)首先在Info.plist中包含所需的后台模式键

2) Check and add following line of code for adding background working of location manager in iOS 9 (update: also works in iOS 10):

2)检查并添加以下代码行,以便在iOS 9中添加位置管理器的后台工作(更新:也适用于iOS 10):

if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    [self.locationManager pausesLocationUpdatesAutomatically:NO];
}

3)Then create a new timer with repeated continuously with every 1 sec.

3)然后创建一个新的计时器,每1秒连续重复一次。

4)In that timer method add these two line of code.

4)在该计时器方法中添加这两行代码。

[self.locationManager stopUpdatingLocation];
[self.locationManager startUpdatingLocation];

This make your app run in background more than 3 mins. To be aware, the battery usage may be costly.

这使您的应用程序在后台运行超过3分钟。要注意,电池使用可能很昂贵。

#2


3  

As of iOS 9, apps are allowed a maximum of 180 seconds (3 minutes) of background execution time upon request. This can be extended indefinitely if you put location updates in the “Required background modes” in your Info.plist. I have verified that doing so lets the background execution run forever. Having this in your Info.plist, however, will require you to get approval from Apple for this background mode before putting your app in the App Store.

从iOS 9开始,根据请求,应用程序最多允许180秒(3分钟)的后台执行时间。如果您将位置更新置于Info.plist中的“所需后台模式”中,则可以无限期延长。我已经验证这样做可以让后台执行永远运行。但是,在将您的应用程序放入App Store之前,将此信息放入您的Info.plist中将要求您获得Apple对此后台模式的批准。

If you don't want to request location background mode, there are some other tricks you can do to get around Apple's restrictions, which you can read about here: https://gooddevbaddev.wordpress.com/2013/10/22/ios-7-running-location-based-apps-in-the-background/

如果您不想请求位置后台模式,您可以采取其他一些技巧来解决Apple的限制,您可以在此处阅读:https://gooddevbaddev.wordpress.com/2013/10/22/ios -7-行驶位置为基础的应用程式功能于该背景/

A word of caution about using those tricks, though -- they are subject to change in any iOS upgrade, and using them might get your app rejected by reviewers.

但是,关于使用这些技巧的注意事项 - 它们可能会在任何iOS升级中发生变化,使用它们可能会使评论者拒绝您的应用。


推荐阅读
author-avatar
鐘彦璋864175
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有