Create cron on iOS

1

I'm trying to make a cron that runs every X time on the iOS system to send some data to the server.

I have read that there is no way to access a cron on a device that does not have a jailbreak. Researching a bit I found that the closest thing to a cron that can be done is to use UILocalNotification or Background Fetch in iOS 7, although with these methods you are at the mercy of the system.

I decided to try the Background Fetch option and followed this guide .

The problem I'm having is that it seems that it does not enter this method and it does not launch ever with the App open or in the background.

What I've tried

I have activated the Background Modes of the app by selecting the Background Fetch option as seen in the following photo:

The code I have in the AppDelegate is the following:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
     NSDate *fetchStart = [NSDate date];
     NSLog(@"Background Fetch: performFetchWithCompletionHandler");

     Utilidades *util = [[Utilidades alloc]init];
     BOOL conexion = [util testInternetConnection];

     //Se comprueba si hay conexión a Internet
     if (conexion) {
         //Envio la información al servidor
         completionHandler(UIBackgroundFetchResultNewData);

     } else {
         NSLog(@"Sin conexión. No se realiza nada");
         completionHandler(UIBackgroundFetchResultFailed);
     }

     NSDate *fetchEnd = [NSDate date];
     NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
     NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}
  

Does anyone know how to make it run every X interval   regardless of whether the application is running or not? In which   Can I be failing?

    
asked by Joacer 13.12.2016 в 17:29
source

1 answer

1

Well, seeing a couple of examples in some search engines, I think the only way to get what you want is just with Background Fetch

In a part of the tutorial that you followed, they tell you that you have to define didFinishLaunchingWithOptions which is basically to define the frequency with the Fetch will be working, in the particular case of that example use UIApplicationBackgroundFetchIntervalMinimum which is a value pre-defined.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    return YES;
}

Then define the function that would be responsible for logically update the data you need to keep that is what you leave in your question performFetchWithCompletionHandler

What you should have in this function is:

-(void) application: (UIApplication * ) application performFetchWithCompletionHandler: (void( ^ )(UIBackgroundFetchResult)) completionHandler {

    NSDate *fetchStart = [NSDate date];

    ViewController * viewController = (ViewController * ) self.window.rootViewController;

    [viewController fetchNewDataWithCompletionHandler: ^ (UIBackgroundFetchResult result) {
        completionHandler(result);
        NSDate *fetchEnd = [NSDate date];
        NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
        NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
    }];
}

And in your ViewController.m define fetchNewDataWithCompletionHandler that must have the logic that is responsible for obtaining the data as it is in the tutorial.

Without forgetting that to prove this you must follow some steps: one)

2) 3)

    
answered by 13.12.2016 / 19:43
source