Is there a way to wakeup suspended app in iOS without user or server intervention

笑着哭i 提交于 2019-11-28 09:06:10

Edit: You have adjusted your question to explicitly state 'without user or server intervention'.

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

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.

Silent Push Notifications

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
}

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);
}

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.

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

Background Fetch

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's documentation on this mentions it's use case:

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.

What you are asking for is against Apple's rule, and I believe no one would like your app doing something secretly in the background without his permission.

And you have listed all the options, that you don't want, but have to count on them.

I think you need to think of how to combine them nicely and user friendly, make them work close what you need.

This is the best combination I can think of:

  • Significant Location Change, you ask for user permission at the first time and occasionally afterwards.
  • Background Fetch, make sure your server return something every time get called, so that iOS won't optimize it to less frequent.
  • Silent Push Notification, you ask for user permission at the first time.
Tapani

You could opt out of multitasking. This way the app never enters background or gets suspended. Just wait for the correct time when the alert should be displayed. However, this probably is not the best solution considering the power usage. Also, the app needs to be active all the time. If the user presses the Home button the app is terminated. But the app does not get terminated when the user locks the device. This is the best thing you can do without using any undocumented apis.

If you do not want your app to run in the background at all, you can explicitly opt out of background by adding the UIApplicationExitsOnSuspend key (with the value YES) to your app’s Info.plist file. When an app opts out, it cycles between the not-running, inactive, and active states and never enters the background or suspended states. When the user presses the Home button to quit the app, the applicationWillTerminate: method of the app delegate is called and the app has approximately 5 seconds to clean up and exit before it is terminated and moved back to the not-running state.

Opting out of background execution is strongly discouraged but may be the preferred option under certain conditions.

https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!