0
votes

I need screen ON/OFF callbacks and voice call callbacks. But i am receiving callbacks when my app is in foreground. But i am unable to get delegate callbacks when my app is in background. How is it possible to get block or delegate callbacks while my app is in background?

I read through apple document http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

and i found "Backgound execution and multitasking" http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20

But nothing helps me for this issue. Please help.

2
did you try these two methods in app delegate, - (void)applicationWillResignActive:(UIApplication *)application; - (void)applicationDidBecomeActive:(UIApplication *)application; - Thilina Hewagama
Unfortunately, screen on/off and call start/end events are not directly available to the app. You have to use the existing application events and create a workaround. - Amar
I tried background execution code also. But it will execute code till the thread is active. When control goes outside thread. Application didn't respond for any events(If app is in background mode). - Mohd Iftekhar Qurashi

2 Answers

0
votes

try use the UILocaleNotification. It can register a notification in the system, and can implement at the specified time.

    -(void) viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredBackground:) name:@"didEnterBackground" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredForeground:) name:@"didEnterForeground" object:nil];
}
 - (void) enteredBackground:(NSNotification*)notification{

        self.notification = [[UILocalNotification alloc] init];

//set the notification property

        [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];


    }


 }
    - (void) enteredForeground:(NSNotification*)notification{
                   //do something
 [[UIApplication sharedApplication] cancelLocalNotification:self.notification];
        }
    } 

In AppDelegate.m

    - (void)applicationDidEnterBackground:(UIApplication *)application
{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didEnterBackground" object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didEnterForeground" object:nil];
}
0
votes

You can run app in background mode for 3 min and all the code will work try

 func registerBackgroundTask() {
            backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
                self?.endBackgroundTask()
            }
            assert(backgroundTask != UIBackgroundTaskInvalid)
        }
        func endBackgroundTask() {
            print("Background task ended.")
            UIApplication.shared.endBackgroundTask(backgroundTask)
            backgroundTask = UIBackgroundTaskInvalid
        }

And just calling self.registerBackgroundTask() makes the app runnable in background for three min.