I am using an NSLog statement in order to understand when -applicationDidBecomeActive: gets called, but it seems that it never gets called. Here is the code:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Some message");
}
The -viewDidLoad method seems to execute the very first time the application starts, but -applicationDidBecomeActive: never gets called. I click on the home button and reactivate the application several times, but still no message from -applicationDidBecomeActive:.
Please help!
Did you put this code in the Application Delegate file?
Related
It is a very simple example. I cannot explain why these lines of code cause a crash. I only want to create multiple threads in a for loop.
In the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
for (int i = 0; i < 5; i++) {
[self performSelectorInBackground:#selector(workInBackground)
withObject:nil];
}
return YES;
}
-(void)workInBackground{
}
The app crashes only sometimes (lldb crash, iPhone Simulator 5.0 - 6.1, Xcode Version 4.6.2). I use ARC.
Can anyone explain this behavior?
Update
I "solved" the problem. When I perform a cleanup before each test, the crash no longer occurs.
I don't know what the problem is, but you should check this out:
What are the differences between didFinishLaunchingWithOption and viewDidLoad
Also you could use NSOperationQueue if you have multiple task that you want to run in background.
Ok, so if you read the apple documetation they say: "This method is called after state restoration has occurred but before your app’s window and other UI have been presented. At some point after this method returns, the system calls another of your app delegate’s methods to move the app to the active (foreground) state or the background state." this mean that actually the app in this point in not yet in the foreground state, here you should store somewhere the intention to start your threads, and after that the sistem call the method - (void)applicationDidBecomeActive:(UIApplication *)application, at this point you are absolutelly sure that your app is in a foreground state and active, now if you have recorded the intention to start your threads you can do it, and don't forget to reset the intention variable status
I want to check every time the app launches whether or not there's a URL in the clipboard, and if so, do something with it. Which method fires that I can override whenever the app launches, whether from a cold launch (it was killed in the background for instance) or if I just press the home button, copy a URL and jump back in.
Is it one of these?
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive
- (void)applicationDidFinishLaunching:(UIApplication *)application
Confused.
As #rmaddy says, the correct method to use once the app launched is applicationWillEnterForeground: from your app delegate.
This method will be called when the user jump backs in, but NOT in other circumstances you don't need to respond to (such as the user receiving a text message and dismissing it).
However, from my testing, applicationWillEnterForeground: is not called when an app is launched from cold; you should catch that in applicationDidFinishLaunchingWithOptions:.
So, basically, your app delegate should include code like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self checkForURL];
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self checkForURL];
...
}
- (void)checkForURL{
//code for checking for URL goes here
}
Hope that helps.
- (void)applicationDidBecomeActive is called when the app is launched or becomes active from the background.
This doc explains everything pretty well: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
With reference to the UIApplicationDelegate Protocol, the handling of app launches can be handled in 2 methods:
application:willFinishLaunchingWithOptions:
application:didFinishLaunchingWithOptions:
And the handling of app launches from background can be handled with the help of method:
applicationDidBecomeActive:
Based on the above call, you can handle your application.
In your app delegate add it to the methods that the other answers have suggested (applicationDidFinishLaunchingWithOptions:). In your root view controller register for the following notification. This will always be called when your application launches once it has already started running.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bringingItBack) name:UIApplicationWillEnterForegroundNotification object:nil];
This will cover both the instances when the app launches and when you are just bringing it back from the background.
I have instantiated locationManager in many of my view controllers. In order to save on battery, I want to stop the locationmanager from updating the location, so I was thinking of calling stopUpdatingLocation inside viewDidUnload in my VCs but viewDidUnload is being deprecated in ios6. Is there a clean way for me to stop all the updatinglocation?
Thanks!
Cuong
You can use the UIApplicationDelegate method :
- (void)applicationWillResignActive:(UIApplication *)application
Inside this method you can call stopUpdatingLocation.
stop Updating the location in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//stop updating Location
}
An earlier answer suggested calling stopUpdatingLocation from applicationWillResignActive. The better solution is to make the call from applicationDidEnterBackground instead.
I've found that calling stopUpdatingLocation from applicationWillResignActive causes the alert that asks the user for permission to use the location to disappear immediately before the user can tap anything.
This occurs because applicationWillResignActive is called when the permission dialog appears. This will call stopUpdatingLocation immediately after startUpdatingLocation, causing the dialog to immediately disappear.
Imagine the app is running and you press the iphone button (the phone button) and you exit the app. then you tap on the app again to enter the app. My problem is that when ever the user does this I want the viewWillAppear or viewDidAppear functions to be called, but unfortunately none of these functions gets called.
I want to know if these function won't get called, then what is the function that is called when the app is appearing again?
How about - (void)applicationDidBecomeActive:(UIApplication *)application in your UIApplicationDelegate?
Look at UIApplicationDelegate. -applicationDidBecomeActive: is what you are looking for.
You can also register for notifications in your classes (UIApplicationDidBecomeActiveNotification). This may be simpler to implement than having your app delegate handle everything since you can have, for example, each view controller manage itself.
(Use NSNotificationCenter's -addObserver:selector:name:object: to register, don't forget to unregister during object cleanup, typically in -dealloc.)
is there anyway to make the app load the first page every time it's opened?
It would be nice if some one can help,
Thanks
You can do this with the AppDelegate
Just add code that reloads to the first page.
- (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.
*/
}
enter in info.plist app key "Application does not run in background" and put for it value YES
In you app delegate, connect your tabBar to an iVar, and do this:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.mayTabBar.selectedIndex=0;
}
This will switch to the first (leftmost) item in the tabBar.