- (void)applicationDidEnterBackground:(UIApplication *)application - ios

Can i use below method somewhere other than AppDelegate ?if yes how?
- (void)applicationDidEnterBackground:(UIApplication *)application

No, But you can have other objects register for the UIApplicationDidEnterBackgroundNotification notification. These objects will then be notified at the same time applicationDidEnterBackground: is called.

This is a method of the UIApplicationDelegate protocol, and can only be implemented by classes that conform to it.
You can set up a notification to other objects in your app from your app delegate by using the NSNotificationCenter object:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:#"didEnterBackground" object:self];
}
There is also the UIApplicationDidEnterBackgroundNotification notification that you can listen for instead of doing the above.
Register the objects that you want to listen for the notification like this:
[[NSNotificationCenter defaultCenter] addObserver:someObject selector:#selector(someMethod:) name:#"UIApplicationDidEnterBackgroundNotification" object:nil];

Related

show a custome view when notification receive in active application

I need to show a universal custom view whenever i received a notification in a active app.
I have created a view , but i am not getting how to show that!
Can anyone help me out.
You have to handle the notification in the AppDelegate methods (usually application(_:​did​Receive​Remote​Notification:​fetch​Completion​Handler:​)) and launch the view in the form it can be shown in any screen of your app.
You can read the docs for UIApplicationDelegate here, section Responding to Notifications and Events.
You will be receiving the push notification in AppDelegate's application(_:​did​Receive​Remote​Notification:​fetch​Completion​Handler:​) from here you can handle.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
// 1. You can call your universal view from here
OR
// 2. You can post this local notification with information
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:#"info"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"showNotification"
object:nil
userInfo:userInfo];
}
For 2nd Option, you will need add observer and receiving method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"showNotification"
object:nil];
-(void)receiveNotification:(NSNotification *)notification {
// call your view
}

application enter in background IOS?

There's a way to notify a object when the method "applicationDidEnterBackground" of application delegate is called but just getting the applicationObject.
I need to do some action when application get on background but I just have access to the application object through "[UIApplication sharedApplication]".
Note: I need those 3 methods applicationWillTerminate, applicationWillEnterForeground, applicationDidEnterBackground but I can't access to applicationDelegate methods.
You can use NSNotificationCenter to inform your class that these methods are being called.
In the init register the the correct notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminateNotification:) name:UIApplicationWillTerminateNotification object:nil];
This will call a method this method, which you will have to add in you class:
- (void)applicationWillTerminateNotification::(NSNotification *)notifictaion{
}
The notification that you want to add are: UIApplicationWillTerminateNotification, UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification
Don't forget to unregistered the you class instance in the dealloc of you class, even in ARC:
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
I had the similar problem. My solution was to get the instance of AppDelegate and register my object as observer:
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate addApplicationDidEnterBackgroundObserver:myobject];
// and later...
[appDelegate removeObserver:myobject];
sure the solution by rckoenes is more elegant.

applicationDidEnterBackground not running

I am putting this in my ViewController.m file and when my app enters the background the NSLog is never called.
Can anyone explain why?
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"Application entered background state.");
}
This is app's delegate method. Put it in the object implementing the UIApplicationDelegate protocol which is by default app's delegate class created for you when you start a new project.
Or use the notification center to get notified about an event of switching to the background. Just register your view controller as an observer of UIApplicationDidEnterBackgroundNotification. It's sent when entering the background.
Example
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

I need to send a message to a method every time my app comes back from background

I'm developing an iOS app with latest SDK.
It's a fullscreen app.
I have a method on viewWillAppear method that has to be called every time the apps comes from background.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setUpVideo];
}
On setUpVideo I set up AVCaptureVideoPreviewLayer because I lose the video when the apps come back from background.
As I have read, viewWillAppear isn't called when the apps come back from background and now, I don't know where to put that code.
On this question, occulus suggest to use [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; but it doesn't work for me.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setUpVideo:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}
Any advice?
Observe UIApplicationWillEnterForegroundNotification instead.
- (void)viewDidAppear {
[super viewDidAppear];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(enterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
// ...
}
- (void)enterForeground:(NSNotification *)notification {
// do stuff
}
Don't call viewWillAppear: directly from the enterForeground: method. Instead move all required code to a separate method and call that from both viewWillAppear: and enterForeground:.
applicationWillEnterForeground will trigger when app comes from background
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
Additionally, you can use UIApplicationDidBecomeActiveNotification for firing some method
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleMethod:)
name: UIApplicationDidBecomeActiveNotification
object: [UIApplication sharedApplication]];
Try posting this notification from
- (void)applicationDidBecomeActive:(UIApplication *)application of AppDelegate(or observe corresponding notification which is better)

Which delegate function is called within a class on tapping the home-button

I have a pop-up which has to be hidden when the user moves away from the class.
On tapping on the home button, the doesn't happen.
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
Other than the above functions is there any other delegate functions which would be called in the same class (not the app-delegate class).
Only the UIApplicationDelegate defines those methods. If you want any other class to handle those events, you need to have the class register for the corresponding notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
And don't forget to remove the observer.
Then you need the method:
- (void)backgrounding {
// App entered background
}

Resources