首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NSTimer 7的背景下保持iOS的存活?

如何在NSTimer 7的背景下保持iOS的存活?
EN

Stack Overflow用户
提问于 2013-09-26 06:47:04
回答 4查看 11.7K关注 0票数 5

我创建了在后台运行NSTimer的应用程序。我使用位置管理器在后台运行NSTimer,

我使用下面的链接在后台运行NSTimer,

How do I get a background location update every n minutes in my iOS application?

这种方法在iOS 6中工作得很好,但在iOS 7上不起作用。我的应用程序在一段时间后崩溃,而应用程序在iOS 7的背景下运行。

请让我知道是否有任何不同的方法在后台中运行NSTimer。

提前谢谢。

EN

回答 4

Stack Overflow用户

发布于 2013-09-26 07:32:28

在iOS7中,有一种新的周期性数据获取模式。将fetch后台模式添加到应用程序中,并在应用程序委托中向- [UIApplication setMinimumBackgroundFetchInterval:传递一个间隔。当应用程序处于后台时,应用程序的委托将开始接收到application:performFetchWithCompletionHandler:的呼叫。

这里有更多信息:ref/occ/intfm/UIApplicationDelegate/application:performFetchWithCompletionHandler

票数 5
EN

Stack Overflow用户

发布于 2016-01-18 07:21:25

代码语言:javascript
复制
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
loop = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(Update) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
票数 2
EN

Stack Overflow用户

发布于 2015-01-03 12:25:28

代码语言:javascript
复制
NSTimer *currentCycleTimer;

UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
[currentCycleTimer invalidate];
currentCycleTimer=nil;
secondsLeft = 120;
currentCycleTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self        selector:@selector(Countdown) userInfo:nil repeats: YES];

-(void) Countdown
{
  [currentCycleTimer invalidate];
  currentCycleTimer=nil;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19021234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档