Skip to content Skip to sidebar Skip to footer

Ios Equivalent To Android Service?

So with iOS 7 supporting a broader background mode, is it possible to finally have an equivalent to Android Service on iOS? What I am after is essentially running app A in the back

Solution 1:

EDIT: Not working as expected. See this answer for best solution: Push Notifications


EDIT: The next solution is only useful while the user is in the app to maintain it synced.

There is no way to perform tasks in the background permanently, but you can use the finite-length tasks to do that, when you make a finite-length, this gonna run always while the app is active, but when you click home button, ios gives you only 10 min to perform your task and invalidate it, but it gives you a chance to make a 'invalidate handler block' where you can do last actions before finish definitely.

So, if you use that handler block to call a finite-length task other time, you can simulate a service by run a task for 10 min and when its end, call its same for other 10 min and consequently.

I use that in a project creating a interface 'Service'. I let you here the code:


  • Service.h

////  Service.h//  Staff5Personal////  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.//  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.//#import <Foundation/Foundation.h>@interfaceService : NSObject@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
@property (nonatomic) NSInteger frequency;
@property (nonatomic, strong) NSTimer *updateTimer;

- (id) initWithFrequency: (NSInteger) seconds;
- (void) startService;
- (void) doInBackground;
- (void) stopService;
@end

  • Service.m

////  Service.m//  Staff5Personal////  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.//  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.//

#import "Service.h"

@implementationService@synthesize frequency;

-(id)initWithFrequency: (NSInteger) seconds{
    if(self= [superinit]){        
        self.frequency = seconds;
        returnself;
    }
    returnnil;
}
- (void)startService{
    [self startBackgroundTask];
}

- (void)doInBackground{
    //EspaƱol //Sobreescribir este metodo para hacer lo que quieras//English //Override this method to do whatever you want
}

- (void)stopService{
    [self.updateTimer invalidate];
    self.updateTimer =nil;
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
    self.backgroundTask =UIBackgroundTaskInvalid;
}

- (void) startBackgroundTask{
    self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:frequency
                                                        target:self
                                                      selector:@selector(doInBackground)
                                                      userInfo:nil
                                                       repeats:YES];
    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundTask];
    }];
}
- (void) endBackgroundTask{
    [self.updateTimer invalidate];
    self.updateTimer =nil;
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
    self.backgroundTask =UIBackgroundTaskInvalid;
    [self startBackgroundTask];
}

@end

With this class i perform my services, but i don't test it for a really long time. The best test i does lasted 16 hours in simulator and everything works fine!

EDIT: That was tested on the simulator, but in phone doesnt work after the application has been terminated.

I let you a example:


// SomeService.h@interfaceSomeService : Service

@end// SomeService.m
#import "SomeService.h"@implementation SomeService

// The method to override
- (void)doInBackground{
    NSLog(@"Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
    NSLog(@"Service running at %.1f seconds", [self getCurrentNetworkTime]);
}
// Your methods- (long) getCurrentNetworkTime{
    return ([[NSDate date] timeIntervalSince1970]);
}

@end

And in your app delegate or where you need to raise the service, you write the next line:

Service myService = [[SomeService alloc] initWithFrequency: 60]; //execute doInBackground each 60 seconds
[myService startService];

And if you need to stop it:

[myService stopService];

May have explained more than necessary, but i want to keep it clear for anyone! I hope its help and sorry for my english.

Solution 2:

No, there is no equivalent to an Android Service. MansApps code does not work, at least not on iOS7. A call of [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask]; in the expiration handler will only return when the app comes back to the foreground, i.e., the call of [self startBackgroundTask]; will not be executed when the app stays in the background.

Solution 3:

Basically it's impossible if your app doesn't implement any of the functionalities listed bellow. And they hardly investigate your app before upload it to the store, you need to justify the use of that permissions

This is what Apple say about that:

Implementing Long-Running Tasks

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

Solution 4:

I found the best and standard solution:

Push notifications

(original post by Matthijs Hollemans, update by Ali Hafizji).

In iOS, apps can’t do a lot in the background. Apps are only allowed to do limited set of activities so battery life is conserved. But what if something interesting happens and you wish to let the user know about this, even if they’re not currently using your app? For example, maybe the user received a new tweet, their favorite team won the game, or their dinner is ready. Since the app isn’t currently running, it cannot check for these events. Luckily, Apple has provided a solution to this. Instead of your app continuously checking for events or doing work in the background, you can write a server-side component to do this instead. And when an event of interest occurs, the server-side component can send the app a push notification! There are three things a push notification can do:

  • Display a short text message
  • Play a brief sound
  • Set a number in a badge on the app’s icon

Tutorial link: http://maniacdev.com/2011/05/tutorial-ios-push-notification-services-for-beginners

Post a Comment for "Ios Equivalent To Android Service?"