Call a web service from iOS at a particular time daily in the background

若如初见. 提交于 2019-12-09 01:52:41

问题


Is there a way to schedule a function which calls a web service at a particular time on a daily basis?

According to my research, I will have to use local notifications or NSTimer to schedule or call the function at a specific time. But I need this to work whether my application is in the background at the schedule time (and not running any background task), or in the foreground.

There are two cases: 1. Location service is running in the background in my application 2. No location or background service is running, or the app is in the suspended state (if the user disabled location updates)

But I have to send some status to my server at the scheduled time daily.


回答1:


You have the following options.

  1. Silent Notification : A silent push notification is pushed from the server, indicating that now is the time, you need to call the webservice and further do the necessary tasks. You will have around 30 seconds to complete the task. Now notice that during this it is not necessary that your app is opened, as this will be done in the background, without your app not opening.

    Follow this link here to configure your project with silent notification

  2. Location based (Geofencing / Location Change) : Well you manage your app by giving it a location, that whenever it enters/ exists / location is updated near to / on that particular location, the app even if terminated, will be launched in the background.

    applicationDidBeginLaunchingWithOptions will be called with a specific key notifying that this app was launched from a location change.

    Follow this link to configure your app accordingly. Now the caveat here is this wont depend on time, but rather a particular location or locations, whichever suits you.

  3. UILocalNotification or Push Notification : But in both cases, its no guarantee that user will interact with the notification banner and open the application in the first place.

Hence in the first option, you can do the work without the user interacting with the device (Device has to be connected to the internet) on Time based and Location based changes and in the second option User has to interact with the device (notification banner). Please choose the one best for your requirement.



来源:https://stackoverflow.com/questions/44017458/call-a-web-service-from-ios-at-a-particular-time-daily-in-the-background

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