iOS Run Code Once a Day

后端 未结 5 914
北海茫月
北海茫月 2020-11-27 03:44

The idea behind this app is very simple: download a file. However this app will be for people who are not always within internet access range, so I need it to know that at,

5条回答
  •  醉梦人生
    2020-11-27 03:59

    You could use local notifications. They execute code when the user opens the notification that is presented. You can set the local notification to recur at a specified interval (e.g. daily, hourly, weekly, etc). This still requires the user to open the app to get the process started.

    UILocalNotification Class Reference

    Once the delegate method fires, you only get a few seconds to execute code. Register for a long running background task, and download whatever you need to do. If it can't finish downloading in the 10 minutes you get for the task, then you need to rethink your download strategy.

    Apple Multitasking and Backgrounding

    We are using this same concept on iOS apps where I work, so this will work if you set it up right.

    UPDATE

    For those curious how this will work, you just need to implement the UILocalNotification delegate methods. They inherit from the UIApplicationDelegate that should already be in place.

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // start your long running bg task here and update your file
    
    }
    

    ** UPDATE 2 **

    Martin H's answer is the most correct so far. But this begs the question, if the user never opens the app, what is the point of downloading data they are never going to see? A recurring local notification reminding them to open the app and update may be the best way, but still requires the user to interact with your app if they want it to remain current and up-to-date.

提交回复
热议问题