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];
Related
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(_:didReceiveRemoteNotification:fetchCompletionHandler:)) 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(_:didReceiveRemoteNotification:fetchCompletionHandler:) 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
}
I've implemented a Notification Center Extension that provides a button to launch its containing app. Depending on the state of the widget I want to navigate to one of two different locations in the app when this button is pressed. I was able to obtain this behavior and it works well while the app is in memory, however, if the app has to be launched for the first time when this button is pressed it does not navigate to the correct location.
I know the reason why this occurs. When I parse the URL to determine where to go in the app delegate, I post a notification and provide information in the userInfo dictionary. I subscribe to this notification in the View Controller that needs to update its UI based on the information in the userInfo dictionary. The problem is, I subscribe to this notification in viewWillAppear. This notification is posted before viewWillAppear is called on this View Controller when the app is launched for the first time, therefore the UI does not update and it behaves as if the app was launched from the home screen.
My question is, is there a different method I can place the code to subscribe to this notification that's early enough in the lifecycle, or is there a better approach to respond to launching from a URL scheme?
Some simplified code:
//AppDelegate.m:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([[url scheme] isEqualToString:#"app name"]) {
if ([[url resourceSpecifier] isEqualToString:#"//tab1"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:DidLaunchWithURLScheme object:nil userInfo:#{#"info": #"first"}];
} else if ([[url query] isEqualToString:#"//tab2"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:DidLaunchWithURLScheme object:nil userInfo:#{#"info": #"second"}];
}
return YES;
}
return NO;
}
//viewWillAppear in View Controller:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didLaunchAppWithURLInfo:)
name:DidLaunchWithURLScheme
object:nil];
- (void)didLaunchAppWithURLInfo:(NSNotification *)notification {
//does get called if app was already in memory
//does not get called if app was fresh launched
//do something here like select a different tab based on the userInfo
}
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];
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!
The app. delegate has the method that gets invoked when the app launches. Pain is my app is not following MVC and I have to reset many text fields to empty when the app. launches(not concerned about first launch). These txt fields are not created in app delegate so i cannot set them in app delegate (as the txt fields are not accessible). how can i know the app has launched in the new class to reset the fields . Is there any condition like
if(application.HasAppLaunced)
{
}
KIndly help
You could register your view controller as an observer of event
UIApplicationDidEnterBackgroundNotification
or
UIApplicationWillEnterForegroundNotification
in your view controller somewhere in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appEnteredBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
and add this method to reset your fields, you can reset all fields here:
- (void)appEnteredBackground{
[textField setText:#""];
}
don't forget to unregister your view controller, put this in some appropriate location:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
I don't completely understand what are you asking for, but if the question is how to detect whather app is active you may use following condition:
[[UIAppication sharedApplication] applicationState] == UIAppicationStateActive
Try This
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Reset your text fields here
}
Try this one.. You can call a method to empty thetextfields using NSNotificationCenter
You can write the following line in AppDelegate.
[[NSNotificationCenter defaultCenter]postNotificationName:#"EmptyFields" object:nil];
You can write the following line in viewDidLoad method.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(setEmpty) name:#"EmptyFields" object:nil];
You can do textfields empty in the following method. You can write it in your ViewController.
- (void) setEmpty{
NSLog(#"Empty....");
}