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.
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.
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;
}
#import "AppDelegate.h"
#import "Scringo/scringo.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
[Scringo initWithAppId:#"" completion:nil];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
--
Is this the correct spot? the sidebar doesn't slide out in the emulator and I'm not getting any errors?… help
Use before return statement,
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Scringo initWithAppId:#"YOUR_SCRINGO_APP_ID" completion:nil];
// Override point for customization after application launch.
return YES;
}
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 {
}
I am new to Objective-C and I'm making my first app, a single-view app. When I run my program ,the fullscreen ad that is supposed to appear (I'm using RevMob) appears. However, when I exit the ad I get a black screen with a blue status bar at the top.
I have tried many things, such as setting my main view controller as initial view controller, restarting my computer, changing/removing debugger, resetting the iOS simulator, etc.
My Xcode version is 4.6 and my OS is mac OSX 10.8.4
I don't want to delete Xcode and I also don't want to remove ads because that is my only source of income.
Here is my code:
Appdelegate.m
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[RevMobAds startSessionWithAppID:#"myappid"];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
RevMobFullscreen *ad = [[RevMobAds session] fullscreen];
[ad loadWithSuccessHandler:^(RevMobFullscreen *fs) {
[fs showAd];
NSLog(#"Ad loaded");
} andLoadFailHandler:^(RevMobFullscreen *fs, NSError *error) {
NSLog(#"Ad error: %#",error);
} onClickHandler:^{
NSLog(#"Ad clicked");
} onCloseHandler:^{
NSLog(#"Ad closed");
}];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
#end
Appdelegate.h:
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic)UIWindow *window;
#end
There is nothing wrong with the ad code. What seems to be (not) happening is that your app has no content. I suspect that you haven't set up the rootViewController (initial view controller) for the app correctly.
This is what you need to do:
Get rid of all of the app ad code and just make an app that works. All it has to do is correctly show an initial view controller. You could just make a new project in XCode using the Single View Application template.
Add your app code as per this example. You will invoke the code from your initial view controller via a button.
Once that is working, you can add the code into - (void)applicationDidBecomeActive:(UIApplication *)application as per your question. When you dismiss the advert, you should see your initial view controller.