如何保持后台运行的 iPhone 应用程序完全正常运行

发布于 2024-09-24 12:06:16 字数 478 浏览 7 评论 0 原文

首先,我知道仅支持 voip、音频和位置应用程序在后台运行,并且它们只会在播放音频或使用位置服务等时运行。

我想知道的是是否有一种让我的应用程序在后台完全运行的方法,对电池寿命的影响无关紧要。

这样,我的应用程序的用户就可以从设置中进行选择,以便在他想要的时候并且只在他希望的时间内保持应用程序的活动状态。例如,如果他正在等待需要应用程序运行的某些内容,则在收到消息后,他可以关闭保持活动功能。

我不知道这是否可能,但我读过一些这样说的帖子,但不幸的是他们没有说如何 =(

更新:在 本教程,我发现 Acrobits 在 Apple Store 上有两个应用程序“可以强制应用程序保持活动状态并处于唤醒状态”在后台”。那么有办法做到这一点吗?

first of all, I know there is only support for voip, audio and location apps to run in background and that they will run just while the audio is been played or while using location services, etc.

What I want to know is if there is a way to keep my app running on background fully operational, doesn't matter the impact on battery's life.

That way the user of my app can select from settings to keep alive the app whenever he wants and just for the amount of time he wish. e.g if he is waiting for something that requires the app to be running, after receiving the messages he can turn off the keep alive functionality.

I don't know if this is possible but I had read some post that say so but unfortunately they didn't say how to =(

UPDATE: In this tutorial, I found that Acrobits has two apps on the Apple Store that "can force the application to stay alive and awake in the background". So there is a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仲春光 2024-10-01 12:06:16

从 iOS 7 开始,应用程序有更新的方式在后台运行。苹果现在认识到应用程序必须不断下载和处理数据。

这是可以在后台运行的所有应用程序的新列表。

  1. 在后台向用户播放音频内容的应用,例如音乐播放器应用
  2. 在后台录制音频内容的应用。
  3. 让用户随时了解自己所在位置的应用,例如导航应用
  4. 支持互联网语音协议 (VoIP) 的应用
  5. 需要定期下载和处理新内容的应用
  6. 从外部配件接收定期更新的应用

您可以声明应用程序受支持使用 X Code 5+ 的 Info.plist 中的后台任务。例如。将 UIBackgroundModes 键添加到应用程序的 Info.plist 文件并向数组添加“fetch”值可让您的应用程序定期从网络下载和处理少量内容。您可以在 XCode 5 中的应用程序属性的“功能”选项卡中执行相同的操作(附加快照)

XCode 5 中的功能选项卡
您可以找到 Apple 文档中有更多相关信息

From ioS 7 onwards, there are newer ways for apps to run in background. Apple now recognizes that apps have to constantly download and process data constantly.

Here is the new list of all the apps which can run in background.

  1. Apps that play audible content to the user while in the background, such as a music player app
  2. Apps that record audio content while in the background.
  3. Apps that keep users informed of their location at all times, such as a navigation app
  4. Apps that support Voice over Internet Protocol (VoIP)
  5. Apps that need to download and process new content regularly
  6. Apps that receive regular updates from external accessories

You can declare app's supported background tasks in Info.plist using X Code 5+. For eg. adding UIBackgroundModes key to your app’s Info.plist file and adding a value of 'fetch' to the array allows your app to regularly download and processes small amounts of content from the network. You can do the same in the 'capabilities' tab of Application properties in XCode 5 (attaching a snapshot)

Capabilities tab in XCode 5
You can find more about this in Apple documentation

依 靠 2024-10-01 12:06:16

在您的应用程序被定向到后台后,您可以在有限的时间内执行任务,但仅限于提供的持续时间。运行时间超过此时间将导致您的应用程序被终止。请参阅“在后台完成长时间运行的任务”部分。

其他人则利用在后台播放音频作为后台进程保持活力的一种手段,但苹果只会在音频播放是合法功能的情况下接受这样的应用程序。 Apple 发布的审查指南第 2.16 条规定:

多任务应用程序只能使用
为他们的预期提供后台服务
用途:VoIP、音频播放、
地点、任务完成情况、本地
通知等

You can perform tasks for a limited time after your application is directed to go to the background, but only for the duration provided. Running for longer than this will cause your application to be terminated. See the "Completing a Long-Running Task in the Background" section of the iOS Application Programming Guide for how to go about this.

Others have piggybacked on playing audio in the background as a means of staying alive as a background process, but Apple will only accept such an application if the audio playback is a legitimate function. Item 2.16 on Apple's published review guidelines states:

Multitasking apps may only use
background services for their intended
purposes: VoIP, audio playback,
location, task completion, local
notifications, etc

终止放荡 2024-10-01 12:06:16

如果任何后台任务运行超过10分钟,则该任务将被挂起,并调用beginBackgroundTaskWithExpirationHandler指定的代码块来清理该任务。后台剩余时间可以通过[[UIApplication共享应用]backgroundTimeRemaining]检查。
最初,当应用程序处于前台时,backgroundTimeRemaining 设置为更大的值。当应用程序进入后台时,您可以看到backgroundTimeRemaining值从599.XXX(1o分钟)减少。一旦backgroundTimeRemaining变为零,后台任务将被挂起。

        //1)Creating iOS Background Task
        __block UIBackgroundTaskIdentifier background_task;
        background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

               //This code block is execute when the application’s 
               //remaining background time reaches ZERO.
          }];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //### background task starts

            //#### background task ends
        });

        //2)Making background task Asynchronous
        if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
        {
            NSLog(@"Multitasking Supported");

            __block UIBackgroundTaskIdentifier background_task;
            background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid;
            }];


        **//Putting All together**
            //To make the code block asynchronous
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                //### background task starts
                NSLog(@"Running in the background\n");
                while(TRUE)
                {
                    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
                    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
                }
                //#### background task ends

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid; 
            });
        }
        else
        {
            NSLog(@"Multitasking Not Supported");
        }

If any background task runs more than 10 minutes,then the task will be suspended and code block specified with beginBackgroundTaskWithExpirationHandler is called to clean up the task. background remaining time can be checked with [[UIApplication sharedApplication] backgroundTimeRemaining].
Initially when the App is in foreground backgroundTimeRemaining is set to bigger value. When the app goes to background, you can see backgroundTimeRemaining value decreases from 599.XXX ( 1o minutes). once the backgroundTimeRemaining becomes ZERO, the background task will be suspended.

        //1)Creating iOS Background Task
        __block UIBackgroundTaskIdentifier background_task;
        background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

               //This code block is execute when the application’s 
               //remaining background time reaches ZERO.
          }];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //### background task starts

            //#### background task ends
        });

        //2)Making background task Asynchronous
        if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
        {
            NSLog(@"Multitasking Supported");

            __block UIBackgroundTaskIdentifier background_task;
            background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid;
            }];


        **//Putting All together**
            //To make the code block asynchronous
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                //### background task starts
                NSLog(@"Running in the background\n");
                while(TRUE)
                {
                    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
                    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
                }
                //#### background task ends

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid; 
            });
        }
        else
        {
            NSLog(@"Multitasking Not Supported");
        }
锦上情书 2024-10-01 12:06:16

要在原生 iOS 设备上运行,请将您的应用程序设为音频播放器/录音机或 VOIP 应用程序,将其设为合法的应用程序以提交到应用程序商店,如果仅供您自己使用,则将其设为假冒应用程序。

即使这样也不会让应用程序“完全可操作”,无论是什么,而是仅限于有限的 API。

For running on stock iOS devices, make your app an audio player/recorder or a VOIP app, a legitimate one for submitting to the App store, or a fake one if only for your own use.

Even this won't make an app "fully operational" whatever that is, but restricted to limited APIs.

红焚 2024-10-01 12:06:16

取决于它做什么。如果您的应用程序占用了太多内存,或者调用了不应调用的函数/类,SpringBoard 可能会终止它。然而,它很可能会被苹果拒绝,因为它不遵循他们的 7 个背景用途。

Depends what it does. If your app takes up too much memory, or makes calls to functions/classes it shouldn't, SpringBoard may terminate it. However, it will most likely be rejected by Apple, as it does not follow their 7 background uses.

嘿看小鸭子会跑 2024-10-01 12:06:16

可能该链接将帮助 bcz 你可能必须在后台运行的应用程序方法中实现 Appdelegate 中的代码..
另请参阅developer.apple.com 网站了解应用程序类
这里是运行应用程序的链接在后台

May be the link will Help bcz u might have to implement the code in Appdelegate in app run in background method ..
Also consult the developer.apple.com site for application class
Here is link for runing app in background

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文