AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
When I press the Home button or the Standby button, the above code gets triggered, enabling my hackish "ScreenState" static class to monitor whether the screen is active or not.
However, when I don't press anything, the screen shuts down after a few minutes and the above functions do not get called, enabling a very annoying bug to appear to my users...
Anyone has an idea?? This app needs to be in the App Store, so no hacks!
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.
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;
}
I am trying to add some RevMob code into my app, the app currently has IAP in it and I am looking to add some advertising (fullscreen).
Now I currently have my code placed in the following method
- (void)applicationDidBecomeActive:(UIApplication *)application {
RevMobFullscreen *fullscreen;
fullscreen = [[RevMobAds session] fullscreen];
fullscreen.delegate = self;
[fullscreen loadAd];
[fullscreen showAd];
}
The problem is that this causes the advert to appear far too often. Ideally I only want to call the advert when the app first loads and then when the user closes the app and then reopens it (standby mode).
The above code is calling the advert code when the user also interacts with the IAP because the alert box is opened asking whether the user wants to buy.
Any Suggestions?
"Ideally I only want to call the advert when the app first loads and then when the user closes the app and then reopens it (standby mode)."
Implement the following methods of the UIApplicationDelegate:
application:didFinishLaunchingWithOptions:
As I'm sure you are well aware, this method fires once when the app is first launched. We have to implement this method because the following method is not called on first launch.
applicationWillEnterForeground:
From the documentation:
This method is called as part of the transition from the background to the active state.
This method is only called when the application moves from a background state. Unlike the applicationDidBecomeActive: method you were experiencing problems with applicationWillEnterForeground: is not called after brief interruptions such as dismissing an incoming SMS or phone call or interacting with In-App Purchases.
Code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[self showAd];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self showAd];
}
- (void)showAd
{
RevMobFullscreen *fullscreen;
fullscreen = [[RevMobAds session] fullscreen];
fullscreen.delegate = self;
[fullscreen loadAd];
[fullscreen showAd];
}
Use
application:didFinishLaunchingWithOptions:
for the initial launch of the app.
For the coming from backward feature record the date time like so
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.timeEnteredBackground = [NSDate date];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSDate* now = [NSDate date];
double diff = [now timeIntervalSinceDate:timeEnteredBackground];
if (diff > SOME_SECONDS)
This way the ad will only appear after a background time greater than some threshold.