如何无限期地每隔1分钟运行一个iOS应用程序? [英] How to run an iOS application every 1 minute infinitely?

查看:68
本文介绍了如何无限期地每隔1分钟运行一个iOS应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个需要每1分钟向服务器发送一次位置和状态更新的应用程序.我尝试了以下方法,但是没有一个方法可以帮助我.有什么解决办法吗?

I have made an application which needs to send Location and status update to server every 1 minute. I tried below ways but none of them helped me out. is there any solution for this?

1- NSTimer -许多人建议这样做.但是问题出在backgroundMode上,并且只能工作20分钟.该应用程序停止发送数据之后.

1 - NSTimer - Many people suggested to do this way. but the problem is going to backgroundMode and it only works for 20 minutes. after that application stops sending data.

2- BackgroundFetchMode -一开始看起来像是正确的解决方案.但是此功能不能保证每1分钟运行一次应用程序.它具有iOS决定应运行哪个应用程序的算法.

2 - BackgroundFetchMode - at beginning looks like the correct solution. But this ability do not guarantee to run application at every 1 minute. it has an algorithm that iOS decide which application should run.

此API与计时器任务不同,系统将决定何时调用处理程序取决于许多约束.因此,如果将timeInterval设置为2.00f(2secs),每2+(最小)秒调用一次处理程序.

This API is not like a timer task, system will decide when to call the handler depending on many constraints.so if you set timeInterval to 2.00f(2secs), handler is called for every 2+(minimum) secs.

3- LocationUpdate -再次以这种方式不起作用,因为它仅在您移动至少500米时才运行您的应用程序.

3 - LocationUpdate - again this way do not works because it only run your application if you move at least 500 meter.

设备移动500米后,应用程序可能会收到通知或之前的通知中的更多内容.它不应该期望通知的频率超过每五分钟一次.如果设备能够从网络,位置管理器中检索数据更有可能及时发送通知.

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

4-推送通知-在使用此方法的文章中,您可以在定义的时间内运行应用程序,并且确实可以运行!

4 - Push Notifications - in an article said in this method you can run an application within defined time and it really does !

您可以在定义的计划时间内安排通知.但问题是向用户显示通知.我需要在背景中保持沉默.

you can schedule a notification within defined schedule time. but the problem is showing notification to user. which I need something silent in Background.

此外,您不能每1分钟运行一些代码.它只是向用户显示通知.用户应该点击您的通知,然后点击didReceiveLocalNotification可用,您可以运行代码.

Also , you can not run some code every 1 minute. it just show a notification to user. and user should tap on your notification and then didReceiveLocalNotification can be available and you can run the code.

推荐答案

如Apple在其

在iOS中,只允许特定的应用类型在后台运行:

In iOS, only specific app types are allowed to run in the background:

  • 在后台播放可听内容的应用程序,例如音乐播放器应用程序
  • 在后台录制音频内容的应用
  • 始终使用户知道其位置的应用程序,例如导航应用程序
  • 支持互联网协议语音(VoIP)的应用
  • 需要定期下载和处理新内容的应用
  • 从外部附件接收定期更新的应用

实现这些服务的应用必须声明其服务支持和使用系统框架来实现以下方面这些服务.声明服务可让系统知道哪个您使用的服务,但在某些情况下是系统框架实际上可以防止您的应用程序被暂停.

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

您可以在链接中阅读有关实现和声明这些后台任务的所有信息.但是,如果您的应用程序是另一种类型的应用程序,或者您无法使用系统框架,则您的应用程序将无法无限期地在后台运行.即使是这样,您也应该始终希望系统出于某种原因(例如重新启动手机)而停止任务.

You can read all about implementing and declaring those background tasks in the link. However, if yours is another type of App, or you cannot use the system frameworks, there is no way for your App to run in the background indefinitely. And even if it is, you should always expect that the system stops your task for some reason (f.e. restarting the phone).

这篇关于如何无限期地每隔1分钟运行一个iOS应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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