且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法在没有用户或服务器干预的情况下唤醒iOS中暂停的应用程序

更新时间:2023-09-07 12:08:16

编辑:您已调整问题以明确说明'没有用户或服务器干预'。

不,据我所知,设计iOS没有提供明确的方法来唤醒您的应用程序确定未来的时间。您可以在后台继续执行长时间运行的任务,机会性地获取更新的内容,提醒用户在需要时重新打开您的应用,并在需要时使用静音推送通知提示前两个。

No, As far as I'm aware by design iOS does not provide an explicit way to wake up your app at determinate time in the future. You can continue long running tasks while in the background, opportunistically fetch updated content, remind users to re-open your app if need be and prompt the first two with silent push notifications if need be.

以下是上述三个选项的一些提示:

Here are some hints on the three options above:

UILocalNotification

最简单的方法是安排一些 UILocalNotifications 未来的某个时间,但为了唤醒您的应用程序,您需要与通知进行交互。这可能不是你想要的。

The easiest way is to schedule some UILocalNotifications at a time in the future but in order to wake up your app you need to interact with the notification. This may not be what you want.

无声推送通知

iOS 7之后的另一个选择是 content-available 或静默推送通知。您为此类通知设置了特定的有效负载,如果您的应用具有正确的 UIBackgroundMode 值设置,它将以静默方式传送到您的应用:

Another option since iOS 7 is a content-available or silent push notification. You setup a particular payload for this kind of notification and if your app has the correct UIBackgroundMode value setup it will be delivered to your app silently:

有效负载看起来像这样:

The payload would look something like this:

{
    "aps" : {
        "content-available" : 1
    },
    "content-id" : 42
}

你会在你的app委托中使用特定的委托方法收到它:

And you would receive it in your app delegate with a specific delegate method:

- (void)         application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
      fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Remote Notification userInfo is %@", userInfo);

    NSNumber *contentID = userInfo[@"content-id"];
    // Do something with the content ID
    completionHandler(UIBackgroundFetchResultNewData);
}

然后,您可以使用此商机下载内容或在需要时快速更新您的应用,看看UIBackgroundModes和后台执行文档有关此方法的更多信息。

You can then use this opportunity download content or update your app quickly if need be, take a look at the UIBackgroundModes and background execution documentation for more info on this approach.

Multi-Tasking 文章也是这种方法的良好开端。

The Multi-Tasking article at Objc.io is also a good start for this approach.

后台提取

如果您阅读后台模式文档可以使用您的应用的 UIBackgroudnModes 值获取机会性地醒来,并有时间下载或更新它的数据。

If you read into the background modes documentation from Apple it's possible using the UIBackgroudnModes value fetch for your app to be woken up opportunistically and given time to download or update it's data.

Apple关于此的文档提到了它的用例:

Apple's documentation on this mentions it's use case:


需要的应用程序定期检查新内容可以要求系统唤醒它们,以便它们可以启动对该内容的获取操作。要支持此模式,请从Xcode项目的Capabilities选项卡的Background modes部分启用Background fetch选项。

Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option from the Background modes section of the Capabilities tab in your Xcode project.