UIApplicationWillEnterForegroundNotification vs application:openURL:sourceApplication:annotation: - ios

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!

Related

Methods not called after openURL function ONLY if app is not active

After visiting a share url on a website, viewers are prompted to open the link in my application. If the application is running, it works perfectly.
However, if the application is closed, the viewDidLoad and viewWillAppear methods are not called on my ViewController, so I am not able to open the desired ViewController.
Does anyone know how to allow to get the viewDidLoad function to run if the app is launched from the openURL function?
I currently have:
AppDelegate:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (!url) { return NO; }
NSString *party_url = [NSString stringWithFormat:#"%#%#/", PARTYURL, url.lastPathComponent];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:#"WebViewNotification" object:party_url]];
return YES;
}
ViewController:
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = [AppDelegate getDelegate];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(webViewNotification:) name:#"WebViewNotification" object:nil];
}
- (void)webViewNotification:(NSNotification *)notification {
NSString *url = [notification object];
PartyViewController *partyViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PartyViewController"];
partyViewController.partyUrl = url;
[self.navigationController pushViewController:partyViewController animated:YES];
}
Again, this works fine if the app was previously opened. However, if it is closed, this does not work. Thank you!
When your app launched from a link without any other operation you don't have a presented ViewController contains this code in your views stacks. so this code will never been executed. consider directly present partyViewController in your CustomTabBarController.

Update UI of app upon launch from URL scheme using notifications

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
}

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

Update a Label in a View when application: openURL: sourceApplication: annotaion: is called

I'm struggling with following problem:
I am using the Method
(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
to set a Serverstring from extern. So when i do call myapp://serverurl/url the string url is set correctly to a NSUSerDefaults Key. That works fine so far.
But when I am currently in the Settingsview of my App, where you can set this variable manually and i call the above url for example from the Mail-App, it sets the variable, but the corresponding Textfield of the View is not updated. So the old string is still written in the Textfield but in NSUSerDefaults its updated correctly.
I tried to work with the Notification Center. I bound the the following Method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateServerURLTextField) name:UIApplicationWillEnterForegroundNotification object:nil];
- (void)updateServerURLTextField
{
NSLog(#"Updating Serveraddress label");
NSString* serverFullURL = [[NSUserDefaults standardUserDefaults] objectForKey:wServerFullURLKey];
self.serverURLTextField.text = serverFullUrl;
}
The Notification gets fired so does updateServerURLTextField but its not updated. I assume some race time conditions?
- (void)applicationDidBecomeActive:(UIApplication *)application
gets called after the Notification is fired. Maybe the GUI cant be updated before the App is active?
Anyone got some hints?
You can't update the UI this way. All changes to UI need to happen on the main thread. You might consider using: PerformSelectorOnMainThread:withObject:waitUntilDone:

iOS event when the application is tapped from home screen

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");
}

Resources