iOS Run Code Once a Day

后端 未结 5 868
北海茫月
北海茫月 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:55

    Have you tried the following?

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0)
        [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:600];
    
    
    - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        completionHandler(UIBackgroundFetchResultNewData);
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 04:05

    Background applications have a set time limit (I believe 10 minutes, but don't quote me on that, it could be less) to complete whatever they are working on. You will not be able to use background tasks to do what you want.

    What you can do is set an NSUserDefault with the date of the last download. On launch check the date saved, if the date is not the current date, and it is after 9:00am, initiate the download programatically.

    0 讨论(0)
  • 2020-11-27 04:06

    Here's the situation regarding background execution and notifications and timers etc. in relation to an app scheduling some activity to happen periodically.

    1. An app cannot execute in the background unless:

      1. It requests extra time from the OS to do so. This is done using beginBackgroundTaskWithExpirationHandler. It is not specified (intentionally) by Apple how long this extra time is, however in practice it is around 10 minutes.

      2. An app has a background mode, the modes are: voip, audio, location, newstand. Even if it has one of these types an app cannot execute without some restrictions. The rest of this discussion assumes the app does not have a background mode.

    2. When an app is suspended it cannot do ANYTHING to rouse itself directly. It cannot previously have scheduled an NSTimer, it cannot make use of something like performSelector:afterDelay. etc.

      The ONLY way the app can become active again is if the USER does something to make it active. The user can do this from via of the following:

      1. Launch the app directly from its icon

      2. Launch the app in response to a local notification that was previously scheduled by the app while it was active.

      3. Launch the app in response to a remote notification sent by a server.

      4. A few others: such as URL launching if the app is registered to deal with launching via a url; or if its registered to be capable of dealing with a certain type of content.

    If an app is in the foreground when a local/remote notification fires then the app receives it directly.

    If the app is not currently in the foreground when a local/remote notification fires then the app DOES NOT receive it. There is no code that is executed when the notification fires!

    Only IF the user selects the notification will the app become active and it can execute.

    Note that the user can disable notifications, either for the entire device, or just for a specific application, in which case the user will never see them. If the device is turned off when a notification is due to fire then it is lost.

    0 讨论(0)
  • 2020-11-27 04:14

    I am pretty sure this is not possible, at most what you can do is send push notifications to the user, so that they manually update when required.

    0 讨论(0)
提交回复
热议问题