I want to keep separate Class "Example: Device Manager" with one property "currentDeviceOrientation". from this how to achieve the following state.
When ever app launch first time i want to update the "currentDeviceOrientation" property.
When ever app coming from background to foreground i want to update the
"current DeviceOrientation" property.
According the value of "currentDeviceOrientation" my rootViewController view should get load from - (void)loadView method.
Kindly give good way to solve my problem.
You can use the already built-in methods in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
Related
According to the official tutorial, the iOS application has several main states, such as foreground, background, and suspended state. An app in use whose view page is at the top of the screen is the foreground state.Then, when the home button is pressed or other applications are used, the APP is in the background state. After that, depending on the mobile phone system resources, the suspended APP may be killed at any time. This is the information that can be found online.
Next was the problem I encountered. After being in the background for a long time, I entered the app I developed again and found that I went directly to the last used view page(The picture was attached below. When using the app in the foreground, the username of the currently logged in user was displayed under the avatar. When returning from the background to the foreground, the username under the avatar became empty). This means that the APP has not been killed, but the data on the page is lost, and some of the data in the memory is partially lost too. It feels that the memory related to the app is partially recovered by the system.This makes me very confused.
I did't do any work about the state preservation and restoration process. I did't need to remember the status information of the previous use of the app when I relaunch the app. All I need is to ensure that the memory occupied by the app is fully restored when the app returns to the foreground from the background.
tips:The background in the expression of returning from the background to the foreground mentioned above, indicates the background or suspended state
view when app back to the foreground
here is my code in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(launchOptions != nil) {
/*remote message push related code*/
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - APNS
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
/**/
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
/*processing code when receiving a message push*/
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
/*processing code when receiving a message push*/
}
#pragma mark - application state
- (void)applicationWillResignActive:(UIApplication *)application {
self.isRunningForeground = NO;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
self.isRunningForeground = YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
My iphone is 6sp running iOS 11. I develop app with ARC.
I need to be able to detect when an App has fully transitioned from background to foreground. I tried using - (void)applicationDidBecomeActive:(UIApplication *)application delegate, but the delegate seems to be called when the application first launches. Then, I tried using - (void)applicationDidBecomeActive:(UIApplication *)application, but it is called before the app has fully transitioned. In the end, I combined the two delegates and it seems to work well for me.
- (void)applicationWillEnterForeground:(UIApplication *)application {
openFromBackground = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (openFromBackground) {
openFromBackground = NO;
// do stuff
}
}
However, I am not sure this is the best way to handle the situation. Any tips or suggestions are appreciated.
It is wired that logs for these two method are not always pairing.
# AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"didFinishLaunchingWithOptions");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
}
If your app is working with background fetch enable, iOS will launch your app as background fetch mode and prepare for data, un-periodically.
Launch due to a background fetch event, didFinishLaunchingWithOptions will called but applicationDidBecomeActive will not get called.
You can duplicate this scenario by turning on option "Launch due to a background fetch event" by edit run scheme in Xcode.
if you are using SceneDelegate try to remove it from your project. In my case I had to:
remove UIApplicationSceneManifest from Plist.
add #property (strong, nonatomic) IBOutlet UIWindow *window in AppDelegate
And add this code in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.frame = [[UIScreen mainScreen] bounds];
[self.window makeKeyAndVisible];
return YES;
}
If you have more doubts about how to remove scenedelegate, check this post:
How to remove Scene Delegate from iOS Application?
- (void)applicationDidBecomeActive:(UIApplication *)application
Is only called when your app is moved from inactive to active state or transitioned to foreground.
So not weird or wrong, everything is as it should be.
What method is called if I start my app, press the home button and then start the app again/switch to it by doublepressing the home button and selecting it? It is not any of the following as they do not print anything when I re-enter the app:
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"Hi");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Hi");
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(#"Hi");
}
No other methods on this site look like they are called when I change to the app, except for trying to add this piece of code but that didn't help;
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
return false;
}
It feels like an extremely easy task but I've been testing all possible methods for 3 hours now. What method is called when I re-enter my application after opening it?
Edit: This has been resolved now, if anyone else is having problems of the same sort this image is super helpful. I found it after resolving my issue: http://www.cocoanetics.com/files/Bildschirmfoto-2012-03-05-um-5.26.29-PM.png
Firstly,
- (void)applicationWillEnterForeground:(UIApplication *)application
method will be called, and after that
- (void)applicationDidBecomeActive:(UIApplication *)application
method will be called after you open the application from the background thread.
In my application, I need to make a call, So obviously my app go to background while making a call using native call feature.if the call goes long my app is getting killed by IOS itself. Now i need to restore the last view at the time of making a call. I have used Native restoration. What i did is
1. Set the restoration ID for all the views and view controllers.
2. Override the app delegate restoration methods.
My Issue is,
If my app go to background and come back to foreground, Last view is displayed using preservation and suddenly moved to main view(Default launch view). just like last view blinking while coming to fore ground.
Here is my setting:
app Delegate code :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Main view settings :
Main storyboard contains the login view as a root. Please guide me to fix the restoration issue.
I managed to get rid of the blinking by making the window key and visible in application:willFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
return YES;
}