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;
}
Related
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.
For this problem, I created a new project with a single view.
I set a url scheme and added a label in the view.
And code is here.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
ViewController *vc = (ViewController *)self.window.rootViewController;
vc.textLabel.text = [NSString stringWithFormat:#"%#\n%#",vc.textLabel.text,url.absoluteString];
return YES;
}
It works fine.
But if I add some time-consuming operations at app initialize,
like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[NSThread sleepForTimeInterval:1];
return YES;
}
Then I found this problem through those steps:
use safari to start the app through input the url scheme
wait it jumps to the app, and press the home button immediately
tap the app icon to continue
get two lines of url on the label
UIApplication calls application:openUrl: multiple times.
I tried using this delegate, same problem.
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
You can press the home button and tap the app icon multiple times, than openUrl will be called multiple times, it depends on cost of time-consuming operations.
I tested on iOS 9.3.3 and iOS 9.3.1, got same problem.
I tried on iOS 8.1.2 and iOS 10.2 not found this problem.
Any ideas/suggestions would be highly helpful & very thankful
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.
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 {
}