Updating App from Background - ios

I have been writing an app that involves updating several values depending on the time the user is in - I update these values using viewDidAppear but unfortunately this function is not being called when the app is loaded again after being sent to the background.
Is there any way to prevent the app from being sent to the background? Or to force the app to open on a certain page after being opened from the background? Or is there a function that is like viewDidAppear, but is always whenever the app is loaded from the background?
Thanks!
Note:
I tried this:
func applicationDidBecomeActive() {
viewDidAppear(false)
}
inside the viewController class, but it didn't work.

You may be looking for applicationWillEnterForeground(application: UIApplication). This method has to be implemented in AppDelegate, not in a controller

Related

Can I detect that the app in not running state is started by background mode?

In Swift 4, I am creating apps related to Beacon and BLE.
Can you distinguish between when a user runs an app and when a system executes an app?
I tried to use willAppear or didAppear because the app's screen appears only when the user is running, but it didn't work as expected.
You need to use AppDelegate, here is the documentationation link. Take a look on
func application(UIApplication, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool
There you have UIApplicatio.LaunchOptionsKey you can check them here.
Also, two useful things that you can use are
func applicationWillEnterForeground(UIApplication)
willEnterForegroundNotification
The second one is a notification for which you can add observer and do something once when you received.
Under AppDelegate :
applicationDidEnterBackground : This method is called to let app know that it is not running in the foreground.
applicationWillEnterForeground : This method is called as a part of the transition from the background to the active state. You should use this to undo any change you made to your app upon entering the background.
applicationDidBecomeActive: method is called soon after this method has finished its execution which then moves the app from the inactive to the active state.

How to know when the user has started an Apple Watch app?

I would like to know when the user starts my Apple Watch app (started from menu or from complication). WKInterfaceController's documentation states that the didAppear method is called when the interface controller content is on screen.
In my simple example project I'm logging all calls to the didAppear method, and I see that it gets called also when the app is not visible on screen.
override func didAppear() {
super.didAppear()
log("didAppear") // Triggered when app not visible
}
My guess is that this has to do with snapshot refreshing, but is there any way to know when the user (not the system) has started my app?
Use applicationDidBecomeActive for this.
From Apple Developer Documentation:
WatchKit calls this method to let you know that your app transitioned from the inactive to the active state. Use this method to start any tasks that were paused or not yet started while the app was inactive.

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.

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.

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

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

Resources