what is the function that is called when the app is appearing? - ios

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.)

Related

Perform action when opening app for the second time

When I open the app it fires the events viewDidLoad and viewDidAppear form my View Controller but when I close it and run it again it does not call any of them.
Any idea?
You need to read up on application states. Here is a link I found online outlining the different states:
http://www.techrepublic.com/blog/software-engineer/understand-the-states-and-transitions-of-an-ios-app/
What you really want is to be notified when your app becomes active.
Probably the easiest way is to implement the function applicationDidBecomeActive() in your app delegate. That will be called when your app becomes active as the foreground app either on launch, or when it returns to the foreground as the active app.
Note that if you want that notification sent to some object other than the app delegate you can listen for the UIApplicationDidBecomeActive notification.

Swift view method call on app exit

I'm currently working on an iOS app with Swift and I've run into a bit of a problem with regards to the view appearing and disappearing handling.
I'm aware that by overriding viewWillDisappear you can handle when within the app the view is being exited out of.
I'm also aware that in the AppDelegate you can implement code in applicationWillTerminate to do certain things when the user exits out of / closes the app.
My question is the following: is there a way for me to know that the application will terminate from my view controller code?
You can have your controller register for a UIApplicationWillTerminate notification, which is delivered under the same conditions as applicationWillTerminate.
See the UIApplicationDelegate docs for more details about the timing of state transitions.

How to disable iPhone 'app not active' flashing banner

My app checks the GPS while my app is not the active app and I use AVAudioplayer too in background.
It works fine and stays in the background doing its thing, but ios7 displays this red top banner with my app name flashing on it when it is not the active app.
How can I disable this banner, it is annoying to use other apps that are squished down 1 line?
I know this can be done as I have other GPS based background apps that don't display this flashing banner.
EDIT - So we found the answer quickly but the solution evades me:
If I stop OpenEars pocketsphinxController from listening with a button that calls this method while the program is active, the banner disappears when the app loses focus:
-(void) mystopListening{
NSLog(#"Tried to stop listening");
[pocketsphinxController stopListening];
}
BUT if I call the same method from my app delegate with (I had to import my view controller.h file in my app delegate.h and add -(void) nystopListening; in my view controller.h to make the below execute properly):
- (void)applicationWillResignActive:(UIApplication *)application{
myViewController * vc = [[myViewController alloc]init];
[vc mystopListening];
}
The banner persists! It is a little like ios7 has decided I am a recording culprit before I even have a chance to turn it off. OR, am I even turning it off?
How do I do this effectively and in what event?
EDIT - So it turns out I am not really turning pocketsphinxController off when 'mystopListening' is called from the app delegate. I know this because it DOES log the 'Tried to stop listening' when called from app delegate but the pocketsphinxController does not respond with its 'pocketsphinxDidStopListening' method. PocketsphinxController does call its 'pocketsphinxDidStopListening' method when I call 'mystopListening' from a button while the app is active.
Why won't the pocketsphinxController respond when called from from the app delegate, I must be doing it wrong?
Thanks,Carmen
Turns out I was not really calling the original pockectsphinxcontroller instance from my app delegate.
As a workaround to the problem I did this:
My app always has a timer running, so in my app delegate where I get notice of when app goes to inactive and comes back active, I just set global flags so my timer can know app active status. Then my timer just uses pockecsphinxcontroller methods to stop and start listening and voila, the banner is no more while app not active.

Termination of an app running in the background

I am currently developing an app that will need to terminate after running in the background for more than five minutes. In order to do this, I will have to have a timer running in the background after the the Home button has been pressed or in case of an interruptions such as an SMS or a telephone call, then, after five minutes the applicationWillTerminate method will be called. My first question is should I put the applicationWillTerminate in the applicationWillResignActive method or in the applicationDidEnterBackground method? My second question is since this is an app with more that one view, Should I write these things in the AppDelegate class or elsewhere? Thank you for your response.
1) You can't force your app to finish programatically.
2) You should never call these AppDelegate methods by yourself. They're meant to be called only by the system.
Reference: UIApplicationDelegate Protocol Reference.
This is pretty ghetto, but what you can do is make your app crash when you want it to exit, and it will close automatically, granted that's not closing the app, but there's no real harm in it as long as you are in control of how it crashes try to go for a bad access error, aka trying to access something that has been deallocated
as for running a timer in the background, i don't know per say if you can do that, but as an alternative you can save the time when they leave the app aka the app goes into the background and then you can have all the events return to your app of the view controller that is first responder, and each UIEvent has a time stamp, and regardless of which event it is you can compare the time stamps and see if it's greater than 5 minutes
Regardless i don't suggest any of the above, but that is the best answer i can come up with for your question
the code for receiving events out side of your app
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
will start the event tracking and the call back is:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event { }
but you have to remember to
[self becomeFirstResponder];
this tells the device which view controller to go to for the event tracking, oh and don't forget to resign first responder, and endReceivingRemotecontrolEvents

Do action upon exit of ios app

How can I assign actions on iOS when a user presses the home button (exit)? I want this for an app where uses user a login feature and I want, upon exit, the user to log out. I dont want to use a button for this.
Use the method,
- (void)applicationDidEnterBackground:(UIApplication *)application
In your application's delegate.
You can't exit an application programmatically, you can use exit(1), however it's not a good practice, chances are there for your apps to get rejected from Apple.
applicationDidEnterBackground will be called for home button press. Try to handle everything here.
Write your actions in appDelegate's "applicationDidEnterBackground" method.

Resources