We have written code to blur the app screen in "applicationWillResignActive" when app goes to app switcher. But the blur affect is observed only some times. Is there is any specific reason for this? We have a doubt that applicationWillResignActive is not always completely executed or is dismissed by OS for other priority activities. Is this is expected behaviour or are we missing anything here?
ScreenBlur is not happening always when app goes to app switcher.
Hard to tell if you don't post any code. Is this Obj-C or Swift? (I'm guessing Obj-C?)
Are you sure applicationWillResignActive
is being called? Does NSLog()
show it being called every time? What if you add logging to all the other state change methods, are they called instead?
Yes, as darkpaw says, it would be helpful to see some code. I'm glad to review any code you have to share. Or, if you can provide a small Xcode project that demonstrates the issue that'd be even better.
@DTS Engineer @darkpaw Here is the sample code
// Add the view to window
-(void)applicationWillResignActive:(UIApplication *)application
{
_coverView = [[UIView alloc]initWithFrame:[self.window frame]];
_coverView.backgroundColor =[UIColor blackColor];
[self.window addSubview:_coverView];
window.bringSubviewToFront(blurView);
}
// Remove the view to window
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(_coverView != nil) {
[_coverView removeFromSuperview];
_coverView = nil;
}
}
And the issue is not seen in sample application. The issue is intermittent in my main application that too only after logging into the application.
Your code looks reasonable. Adding some logging statements to your -applicationWillResignActive:/-applicationDidBecomeActive: methods will help isolate this problem.
We have tried with log statements. Even during the issue we are able to see the logs in applicationWillResignActive. applicationWillResignActive is being called everytime but the blur view is not shown everytime. Any reason that the UI work is not being done?
we have put logs at the entry of applicationWillResignActive and also at the exit of the code. Those logs are always getting , but the blur code in between the logs is not working. We have a doubt that applicationWillResignActive is called but the UI part is not being executed or is dismissed by OS for other priority activities. Is this is expected behaviour or are we missing anything here?
@DTS Engineer @darkpaw , Any info on this behaviour. And a new observation is that the issue is intermittent in iphone15 , and its continuously seen in iphone14. Any point to focus on?
// Add the view to the window
-(void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"applicationWillResignActive"); // Confirm this method is being called
NSLog(@"[self.window frame] = %@", NSStringFromCGRext([self.window frame])); // Confirm the frame is valid
_coverView = [[UIView alloc]initWithFrame:[self.window frame]];
NSLog(@"applicationWillResignActive: Created view");
_coverView.backgroundColor =[UIColor blackColor];
[self.window addSubview:_coverView];
window.bringSubviewToFront(blurView);
NSLog(@"applicationWillResignActive: View added and brought to the front");
}
// Remove the view from the window
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"applicationDidBecomeActive"); // Confirm this method is being called
if(_coverView != nil) {
NSLog(@"applicationDidBecomeActive: _coverView is not nil, so removing it, and setting it to nil");
[_coverView removeFromSuperview];
_coverView = nil;
} else {
NSLog(@"applicationDidBecomeActive: _coverView is already nil");
}
}
You haven't provided us any evidence so far that you've added the logging to these methods, and so we can't tell exactly what the sequence of events is, so it's extremely difficult for us to debuig this from afar with little info.
Put the comments in as I have above, and run it through until you see the problem occur, then copy the relevant text from the console and paste it here.
@darkpaw @DTS Engineer
- (void)applicationWillResignActive:(UIApplication *)application {
if (!_coverView) {
_coverView = [[UIView alloc] initWithFrame:self.window.bounds];
_coverView.backgroundColor = [UIColor blackColor];
_coverView.userInteractionEnabled = NO;
[self.window addSubview:_coverView];
[self.window bringSubviewToFront:_coverView];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSString *timeString = [formatter stringFromDate:[NSDate date]];
NSLog(@"[%@] [AppDelegate] Cover view added immediately", timeString);
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (_coverView) {
[_coverView removeFromSuperview];
_coverView = nil;
NSLog(@"[AppDelegate] Cover view removed");
}
}
This is the code that we have used and the logs are
Idletimer canceled ...
[2025-05-20 17:02:06.518] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:13.976] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:17.442] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:20.217] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:23.017] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:25.304] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Idletimer canceled ...
[2025-05-20 17:02:27.719] [AppDelegate] Cover view added immediately
[AppDelegate] Cover view removed
Even when the screenblur is not happening we see this log "[AppDelegate] Cover view added immediately"
You've shown the time the cover view is added, but not the time it was removed.
How long did a user take between putting the app into the app switcher (or background), and bringing it back to active? How long between those NSLogs?
You're giving us piecemeal information here, making it difficult to help you.
Also, note that I added an else
to where you're checking for a nil
. That's important, because if coverView
isn't nil
at that time, then it's not going to create the view.
Add logging to every bit that's relevant.
I've since moved onto Swift and SwiftUI, but when one of my apps was written in Objective-C this is what I used, and it worked every time. Perhaps it's because I'm using a UIImageView
?:
- (void)applicationWillResignActive:(UIApplication *)app
{
NSLog(@"AD: applicationWillResignActive");
[self showBlurView:YES];
}
- (void)applicationWillEnterForeground:(UIApplication *)app
{
NSLog(@"AD: applicationWillEnterForeground");
[self showBlurView:NO];
}
- (void)applicationDidBecomeActive:(UIApplication *)app
{
NSLog(@"AD: applicationDidBecomeActive");
[self showBlurView:NO];
}
- (void)showBlurView:(BOOL)show
{
NSLog(@"AD: showBlurView: %@", show ? @"YES" : @"NO");
if(show) {
blurView = [[UIImageView alloc] initWithFrame:self.window.frame];
blurView.image = [UIImage imageNamed:@"blurViewImage"];
[self.window addSubview:blurView];
} else {
if(blurView != nil) {
[blurView removeFromSuperview];
}
}
}
Hello @Temenos. Thank you for providing some code listings. I can't see enough to commend based on that. You mentioned that this issue only occurs in your main application and not in a sample. If that is the case then perhaps this problem is related to something else in your app and may require additional debugging. And, you may need to do some strategizing about how you go about doing that. If you're unable to make a new Xcode project and add in a couple of APIs to reproduce the problem, perhaps start with a copy of the sources for the app where you can reproduce the problem and incrementally remove stuff between tests until you are no longer able to reproduce the problem. That will help you narrow down the source of the problem.
Hi @DTS Engineer, can you please clarify these doubts:
- We are using appdelegates in our code, is it possible that scenedelegate works faster and are more consistent?
- The same issue can be seen with phonepe app. We have compared with hdfc, imobile apps which are working fine always. Can we tag this inconsistent screen blur issue to appdelegate/scenedelegate performance??
- I have tried with UIView, UIImageView, UIVisualEffectView, UIWindow but the issue exists.