iOS,ObjC:后台服务 运行

iOS, ObjC: Service running in background

我一直在构建一个流媒体应用程序,我必须在那里实现一个必须在午夜开始并在早上结束的系统。我的问题是我不知道如何实现它。

该系统必须在应用程序启动时保持监视并继续 运行ning,即使应用程序进入后台也是如此。

与此同时,我尝试实现一个计时器,但它 运行 只持续一段时间,一段时间后就会消失。我的应用程序不是 VoiP 或 StandApp。据官方说法,我无权使用 运行 无限服务。无论如何,我需要一个在凌晨 12 点调用方法并开始文件下载的解决方案。

//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //run function methodRunAfterBackground
    NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(isNightFrame) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
});

然后我尝试玩 UILocalnotifications 但似乎必须有用户迭代。他必须点击通知。

我需要一些建议该走哪条路以及如何把事情做好。先感谢您。

在非越狱设备上,无法实现你想做的事情
如果您的应用未使用 Apple 提供的任何后台模式,则在 iOS.
暂停应用后,您将无法触发任何代码执行 我猜你使用的是 "App plays audio" 后台模式。即使启用此模式,如果您的应用未真正播放音频,应用也会被暂停。

TL;DR:没有用户交互无法恢复暂停的应用 在非越狱设备上。

编辑: 实际上,使用 Background Fetch 可以执行一些代码,但我很确定 iOS 永远不会让您从 background fetch 为已暂停的应用启动音频会话。

编辑 2: 根据 this article,您不能选择触发 Background Fetch。 iOS 决定为您的应用触发它。

How frequent your application can perform a background fetch is determined by the system. It depends on factors such as:

Whether network connectivity is available at that particular time
Whether the device is awake
How much data and time your application has taken in its previous attempt to perform a background fetch
In other words, your application is entirely at the mercy of the system to schedule background fetch for you.
In addition, each time your application uses background fetch, it has at most 30 seconds to complete the process.

iOS 可以决定在您的应用收到 Push/Local 通知后触发 Background Fetch。但它绝对不可靠(完全不可靠)。