iOS:让应用程序像服务一样运行 [英] iOS: Keep an app running like a service

查看:204
本文介绍了iOS:让应用程序像服务一样运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中,如果操作系统不再位于前台,我将如何指示操作系统保持我的应用程序运行?

In iOS, how would I instruct the OS to keep my app running even if it is no longer in the foreground?


  • < a href =https://itunes.apple.com/us/app/skype-for-iphone/id304878510?mt=8 =nofollow noreferrer> Skype

  • Viber

  • Empatica

  • Zenly

  • Skype
  • Viber
  • Empatica
  • Zenly

此外还有更多应用。

推荐答案

基本上没有iOS中的服务类型应用程序或功能。
即使是后台应用程序(UIBackgroundMode)也无法完全自由运行,并且没有其他操作系统上的服务或守护程序等限制。

Basically there is no such thing as a service type app or functionality in iOS. Even the "background" apps (UIBackgroundMode) cannot run entirely freely and without restrictions like a service or daemon etc. on other OSs can.

这是情况关于后台执行和通知以及计时器等。

Here's the situation regarding background execution and notifications and timers etc.

1)应用程序无法在后台执行,除非:

1) An app cannot execute in the background unless:

a)它要求OS提供额外的时间。这是使用beginBackgroundTaskWithExpirationHandler完成的。苹果没有(故意)指定这个额外时间有多长,但实际上它大概是10分钟。

a) it requests extra time from the OS to do so. This is done using beginBackgroundTaskWithExpirationHandler. It is not specified (intentionally) by Apple how long this extra time is, however in practice it is around 10 minutes.

b)应用程序有后台模式,模式是:voip,audio,location,newstand。即使它有这些类型之一,应用程序也无法无限制地执行。本讨论的其余部分假定应用程序没有后台模式。如果您尝试使用这些后台模式之一来使您的应用能够在后台运行,但您的应用未合法使用特定功能,那么您的应用将在应用商店提交时被拒绝(即拥有UIBackgroundMode它必须是:一个voip应用程序,需要持续的位置更新,在后台连续播放音频的能力是一个基本功能,或者是一个新的应用程序)。

b) an app has a background mode, the modes are: voip, audio, location, newstand. Even if it has one of these types an app cannot execute without restrictions. The rest of this discussion assumes the app does not have a background mode. If you try to use one of these background modes to enable your app to be capable of running in the background but your app does not make legitimate use of the specific functionality then your app will be rejected upon app store submission (i.e. to have a UIBackgroundMode it MUST be either: a voip app, NEED to have continual location updates, the ability to play audio in the background continuously is a fundamental feature, or be a newstand app).

2)当一个应用程序被暂停时,它无法做任何事情直接引发自己。它以前不能预定NSTimer,它不能使用像performSelector:afterDelay之类的东西。等等。

2) When an app is suspended it cannot do ANYTHING to rouse itself directly. It cannot previously have scheduled an NSTimer, it cannot make use of something like performSelector:afterDelay. etc.

应用程序再次变为活动状态的唯一方式是,如果USER执行某些操作使其处于活动状态。用户可以通过以下方式执行此操作:

The ONLY way the app can become active again is if the USER does something to make it active. The user can do this from via of the following:

a)直接从其图标启动应用程序

a) Launch the app directly from its icon

b)启动应用程序以响应应用程序在活动时先前安排的本地通知。

b) Launch the app in response to a local notification that was previously scheduled by the app while it was active.

c)启动应用程序以响应发送的远程通知通过服务器。

c) Launch the app in response to a remote notification sent by a server.

d)其他几个:如果应用程序已注册处理通过URL启动的URL启动;或者如果它已注册能够处理某种类型的内容。

d) A few others: such as URL launching if the app is registered to deal with launching via a url; or if its registered to be capable of dealing with a certain type of content.

如果当本地/远程通知触发时应用程序位于前台,则应用程序会收到该消息直接。

If an app is in the foreground when a local/remote notification fires then the app receives it directly.

如果本地/远程通知触发时应用程序当前不在前台,则应用程序不会收到它。通知触发时没有执行代码!

If the app is not currently in the foreground when a local/remote notification fires then the app DOES NOT receive it. There is no code that is executed when the notification fires!

只有当用户选择通知时,应用才会变为活动且可以执行。

Only IF the user selects the notification will the app become active and it can execute.

请注意,用户可以为整个设备禁用通知,也可以仅针对特定应用程序禁用通知,在这种情况下,用户将永远不会看到通知。如果设备在通知发生时关闭,则会丢失。

Note that the user can disable notifications, either for the entire device, or just for a specific application, in which case the user will never see them. If the device is turned off when a notification is due to fire then it is lost.

1)有一些新的背景模式,例如背景提取(但你仍然无法以确定的方式被操作系统唤醒)

1) there are some new background modes such as background fetch (you still can't be roused by the OS in a deterministic fashion however)

2)现在有了后台推送通知

2) there's now a background push notification

3)beginBackgroundTaskWithExpirationHandler时间从10分钟减少到大约3分钟。

3) beginBackgroundTaskWithExpirationHandler time has reduced from 10 minutes to about 3.

这篇关于iOS:让应用程序像服务一样运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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