iOS event when the application is tapped from home screen - ios

I am developing an iOS application and I need to catch the event when the application is opened by tapping the icon of the application at home screen.
Does anyone know what is the name of this event?
Thank you.

In the application delegate you can implement the following methods to catch up with this events:
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application

Add observer in your class
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillEnterInForeground) name:UIApplicationWillResignActiveNotification object:nil];
- (void)applicationWillEnterInForeground
{
NSLog(#"App enter in foreground");
}

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
}

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];

NSNotification centre to observe whether an application is opened through URL Scheme

I have a class which will make NSNotificationCenter to observe whether the app is active or it went to background and it will do a set of actions according to that.
I want to set a NSNotificationCenter to observe whether the app is opened through URL scheme .
This is what I tried:
[[NSNotificationCenter defaultCenter] addObserver:[self class] selector:#selector(appOpenedThroughUrl:) name:UIApplicationLaunchOptionsURLKey object:nil];
But its not getting called when the app opens through URL please provide me some idea to do the same.
I don't want to use the delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
+(void)appOpenedThroughUrl:(NSNotification *)notification {
NSLog(#"%#",notification.userInfo);
}
you need to post notification whenever it open through custom url scheme
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationLaunchOptionsURLKey object:self];

UIApplicationWillEnterForegroundNotification vs application:openURL:sourceApplication:annotation:

In UIViewController I register for UIApplicationWillEnterForegroundNotification notification.
-(void)viewDidAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(btnContinuePressed:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
I want to execute btnContinuePressed: only if the application was resumed in a normal way - clicking the icon or opening via multitasking menu.
Method btnContinuePressed: should not be executed when the application was opened using URL scheme. Opening via URL scheme is handled in AppDelegate using custom notification:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
[[NSNotificationCenter defaultCenter] postNotificationName:#"didOpenViaUrl"
object:url];
return YES;
}
Bottom line: notification UIApplicationWillEnterForegroundNotification should not be triggered if didOpenViaUrl was.
In simple word you can not make difference them but you can do a tricky way to achieve by creating a BOOL property in your appDelegate code which will tell you from where the application has been opened I think!

- (void)applicationDidEnterBackground:(UIApplication *)application

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];

Resources