Tamil_Arya Tamil_Arya - 1 year ago 69
iOS Question

Call the server every minute while app in Foreground

i want to call my server every minute while app in foreground.this call should not be depends on any ViewController class as be common method.

(ex : call my server depends on server response i want to show some alert as commonly )

i just keep trying by app delegate method but i couldn't get any solution.
is there any other ways or any method belongs to app delegate ??

Answer Source


You should not dump all your code in AppDelegate just because it is singleton and availabe through out the application life cycle. Putting everything in appdelegate will make your code very much clumsy and follows a very bad design pattern.

Following MVC is one nice thing you can do to keep your code reliable and robust.

Anyway Here is what you can do,

I believe you must be having a singleton class for making webservice calls. If not create one.

For Example lets call the class as WebService.h and WebService.m

So your WebService.h should look like

@interface WebService : NSObject
+ (instancetype)shared; //singleton provider method
- (void)startSendPresence; //method you will call to hit your server at regular interval
- (void)stopSendPresence //method you will call to stop hitting

WebService.m should look like

@interface WebService ()
@property (strong, nonatomic) NSTimer *presenceTimer;

@implementation WebService

+ (instancetype)shared
    static id instance_ = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance_ = [[self alloc] init];

    return instance_;

- (void)startSendPresence {

    [self sendPresence:nil]; //to make the first webservice call without waiting for timer to trigger

        self.presenceTimer = [NSTimer scheduledTimerWithTimeInterval:self.presenceTimerInterval target:self selector:@selector(sendPresence:) userInfo:nil repeats:YES];

- (void)sendPresence:(NSTimer *)timer {
    //make your web service call here to hit server

- (void)stopSendPresence {
    [self.presenceTimer invalidate];
    self.presenceTimer = nil;

Now your Webservice singleton class to hit webserver at regular interval is ready :) Now call it wherever you want it when you want to start hitting and call stopSendPresence when you want to stop it :)

Assuming you want to start hitting server as soon as Application comes to foreground always (Though does not make much sense to me hope it makes to you)

In your AppDelegate.m

//this method will be called when you launch app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[WebService shared] startSendPresence];

//this method will be called when you brimg it foreground from background
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[WebService shared] startSendPresence];

If you want to stop hitting sever as soon as your app goes to background

- (void)applicationDidEnterBackground:(UIApplication *)application {
     [[WebService shared] stopSendPresence];

Hope this helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download