Since apple watch is just an interface. Can I use background refresh to update the apple watch screen while the app is in the background mode not in foreground?
And if yes, how can I do that?
You can change the screen when for some reason a page deactivates, like for example, switching to another page. You should be using the method:
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
Which is on every WKInterfaceController class.
Related
I encountered this situation where I had added app state restoration APIs for the first time to a new app supporting iOS 9.3 and higher. The state restoration is working fine functionally, but I noticed that it was showing a screen snapshot instead of the LaunchScreen.xib content because of the delay during state restoration. By snapshot I'm referring to the automatic screenshot iOS takes of your apps UI as it goes into the background.
If you don't know what app state restoration is, it came out with iOS 6, here's the link from Apple on it:
Apple Documentation on App State Saving & Restoring
Showing the screen snapshot in this apps case is a significant problem as this particular app has about 4 seconds of delay, on an iPhone 4s running iOS 9.3.5, to do during app state restoration. Users would perceive the app to be hung since the launch / splash screen was not being shown during this time. This situation reproduces on all iOS versions currently available, on both simulator and device.
How can I prevent the snapshot from being shown, and force the LaunchScreen.xib to be used always, all while still preserving the app state save/restore functionality benefits?
After researching this I found that Apple has long ago provided a method off of UIApplication to deal with this situation. But its usage, even today, is poorly documented.
The solution is to use the ignoreSnapshotOnNextAppliationLaunch method from UIApplication.
Apple ignoreSnapshotOnNextApplicationLaunch method
You will have to access it via the UIApplication singleton pattern as suggested by Apple here as I will explain:
Apple UIApplication sharedApplication method
The where to use this is what is not clearly documented and that I am sharing here. The ignoreSnapshotOnNextApplicationLaunch method will have absolutely no effect unless specifically called when iOS is saving the app state from the view controller(s). Such as when you tap the home button to background the app.
You cannot call this method directly from the AppDelegate methods dealing with background / foreground transitions, as it needs to be called from the view controllers while their states are being saved for later restoration.
For this saving task Apple provides the encodeRestorableStateWithCoder method from UIViewController
Apple encodeRestorableStateWithCoder method
And this is where we need to make the change. If doing state restoration you should already have it; but by adding this method call to each view controller class where you have setup restoration IDs in storyboard, or are saving state manually, you can avoid any snapshots being used by including the ignoreSnapshotOnNextApplicationLaunch from the UIApplication singleton. This will not prevent iOS from taking the snapshot, just not showing it during app state restoration on re-launch.
// save any app state information that is not already saved automatically
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
// prevent taking a screen shapshot and force launchScreen xib to be used always
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
[super encodeRestorableStateWithCoder:coder];
return;
}
Be sure you re-background the app after adding this during your testing, to have iOS delete the previously saved snapshot file.
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
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.
Long story short, I'm trying to change my iOS app's rootViewController on applicationWillEnterForeground:, like so:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
MyViewController *controller = [[MyViewController alloc] init];
self.window.rootViewController = controller;
}
However, when iOS performs the "zoom in" animation that is performed when an app is moved from the background to the foreground, it still shows the previous rootViewController's view. Then, as soon as the animation is complete, the app blasts the new rootViewController's view onto the screen.
One way to solve this is to simply move that code to - (void)applicationDidEnterBackground:, but the problem with this solution is that, in my app, there is no way to tell if a new rootViewController will be assigned until - (void)applicationWillEnterForeground:(UIApplication *)application (it is based on time passed since leaving the app).
How can I force the app to redraw before iOS performs the animation taking the app from the background to the foreground?
I believe this is not possible. The screen that iOS shows of your app when it comes into the foreground is actually a screenshot the system took when the app went into the background. There is no way to manipulate or replace that image at the time the app comes back into the foreground.
This behavior is partly documented in the Moving to the Background section of the iOS Application Programming Guide:
Apps can use their applicationDidEnterBackground: method to prepare for moving to the background state. When moving to the background, all apps should do the following:
Prepare to have their picture taken. When the applicationDidEnterBackground: method returns, the system takes a picture of your app’s user interface and uses the resulting image for transition animations. If any views in your interface contain sensitive information, you should hide or modify those views before the applicationDidEnterBackground: method returns.
Apple does not explicitly document that you cannot modify or replace this screenshot at a later time but neither do they say the opposite anywhere I know of.
In my application, When the application goes to background I am calling a Passcode page (Passcode Page which does authentication).
My requirement is when the user launch the app from the foreground he will see the passcode page. If he enters the correct passcode then only he can see the rest pages.
In delegate.m file
- (void)applicationDidEnterBackground:(UIApplication *)application
{
PasscodeViewController *passcodeController = [[PasscodeViewController alloc] initWithNibName:#"PasscodeViewController" bundle:nil];
[navController pushViewController:passcodeController animated:YES];
}
When I am launching the application from the background then It is showing me the previous page( from which page I came to background ) for a fraction of second and after that Passcode page comes.
But I want to hide my confidential information from others (who doesn't know the passcode) that are shown in the previous page.
It is correctly working in Simulator but in Device it is not working properly.
Can you please guide me in that ?
OR
Is it the normal behavior of the iOS device ? What ever the page transition it will do, it will perform while the application is running in foreground.
I am not sure about that. Please tell me where I went wrong.
Thank you.
Every app I've used with a similar feature has operated as you describe, with the fractional-second flash before the lock view appears.
I think it's a matter of when UIKit thinks it needs to re-render... We had a similar case with a splash screen, but using applicationDidEnterBackground for adding the splash helped.
My idea is to avoid the animating, using
[navController pushViewController:passcodeController animated:NO];
Whenever your app goes background add a UIView with white background.
Whenever your app comes up push your PasscodeViewController view on top
Please add observers for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification to do the above functionality
Also be sure to remove the Observers when your view disappears
When user enters correct passcode remove the UIView.
Try applicationWillResignActive:
- (void)applicationWillResignActive:(UIApplication *)application
{
PasscodeViewController* passcodeController = [[PasscodeViewController alloc] initWithNibName:#"PasscodeViewController" bundle:nil];
[navController pushViewController:passcodeController animated:YES];
}
When Application goes to background push the passcode viewcontroller to navigationcontroller in the delegate applicationDidEnterBackground because there will be that fractional flash almost all time u can have the passcodecontroller pushed before entering background.