I want to make an app where I can have a task (GET URL) run at predefined times (selectable in UI). For instance, Monday to Friday at 8am.
我想创建一个应用程序,在其中我可以让一个任务(GET URL)在预定义的时间运行(在UI中可选)。比如,周一到周五早上8点。
Is this possible in iOS?
这在iOS中可能吗?
I tried searching but haven't found anything very useful, probably using the wrong search terms. Does anyone happen to find some sample code for what I'm trying to do?
我试过搜索,但没有发现任何有用的东西,可能是用了错误的搜索词。有没有人碰巧找到我要做的东西的样本代码?
Edit: Pointing out that I want the app to perform these tasks even if the app is not running. I want to user to just select wanted days of the week and time, and then the phone will take care of everything - even if the phone is restarted.
编辑:指出我希望应用程序执行这些任务,即使应用程序没有运行。我想让用户选择一周的天数和时间,然后手机就会处理所有的事情——即使手机重新启动。
3 个解决方案
#1
4
If you want to regularly wake up to download content, you can register to get push notifications, and download based on the contents of the notification. You are likely to get some cycles to do this close to the scheduled time. If you want to 'opportunistically' download content you can register for background 'fetch' but there is no guarantee of scheduling.
如果您想要定期醒来下载内容,您可以注册以获得推送通知,并根据通知的内容进行下载。您可能会得到一些循环来接近预定的时间。如果你想要“有机会”下载内容,你可以注册后台“取回”,但没有调度的保证。
参见https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
#2
1
So, first of all, the app has to be open for any kind of task to run. After that, there are 2 ways you can do it:
所以,首先,应用程序必须是开放的,可以运行任何类型的任务。在那之后,有两种方法可以做到:
You can set a timer with a selector, or you can use grand central dispatch. Both have their strengths and weaknesses depending on what the task is...
可以使用选择器设置计时器,也可以使用grand central dispatch。两者的优缺点取决于任务是什么……
https://developer.apple.com/Library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/index.html
https://developer.apple.com/library/ios/documentation/performance/reference/gcd_libdispatch_ref/index.html
check out those links and see if they can help.
看看这些链接,看看是否有帮助。
Remember that you should do stuff like this on a background thread, and that the UI cannot be updated from any thread by the main thread. Additionally, remember the limitations iOS puts on background applications.
请记住,您应该在后台线程上执行这样的操作,并且主线程不能从任何线程更新UI。此外,还记得iOS对后台应用程序的限制。
#3
0
Yes, it is possible. You can use -
是的,这是可能的。您可以使用- - - - - -
[self performSelector:@selector(myFunc:) withObject:nil afterDelay:5.0];
#1
4
If you want to regularly wake up to download content, you can register to get push notifications, and download based on the contents of the notification. You are likely to get some cycles to do this close to the scheduled time. If you want to 'opportunistically' download content you can register for background 'fetch' but there is no guarantee of scheduling.
如果您想要定期醒来下载内容,您可以注册以获得推送通知,并根据通知的内容进行下载。您可能会得到一些循环来接近预定的时间。如果你想要“有机会”下载内容,你可以注册后台“取回”,但没有调度的保证。
参见https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
#2
1
So, first of all, the app has to be open for any kind of task to run. After that, there are 2 ways you can do it:
所以,首先,应用程序必须是开放的,可以运行任何类型的任务。在那之后,有两种方法可以做到:
You can set a timer with a selector, or you can use grand central dispatch. Both have their strengths and weaknesses depending on what the task is...
可以使用选择器设置计时器,也可以使用grand central dispatch。两者的优缺点取决于任务是什么……
https://developer.apple.com/Library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/index.html
https://developer.apple.com/library/ios/documentation/performance/reference/gcd_libdispatch_ref/index.html
check out those links and see if they can help.
看看这些链接,看看是否有帮助。
Remember that you should do stuff like this on a background thread, and that the UI cannot be updated from any thread by the main thread. Additionally, remember the limitations iOS puts on background applications.
请记住,您应该在后台线程上执行这样的操作,并且主线程不能从任何线程更新UI。此外,还记得iOS对后台应用程序的限制。
#3
0
Yes, it is possible. You can use -
是的,这是可能的。您可以使用- - - - - -
[self performSelector:@selector(myFunc:) withObject:nil afterDelay:5.0];