Does your app have sensitive information that belongs to your user? If so, you’re probably taking some action to protect it. Storing it with encryption, locking it behind a passcode, using TouchID, clearing their session when they leave the app, etc.
One thing you might not have considered is a vulnerability when using the app switcher. Could someone take your user’s phone and view the sensitive information by just double tapping the home button?
Let’s protect that data. We’re going to put a blurred view over the app whenever the user leaves (or even just hits the app switcher right away), plus it can look pretty cool!
In our AppDelegate.cs
, override the OnResignActivation
method:
public override void OnResignActivation(UIApplication uiApplication) { var window = UIApplication.SharedApplication.KeyWindow; var blurView = UIBlurEffect.FromStyle(UIBlurEffectStyle.Light); var blurEffectView = new UIVisualEffectView(blurView); blurEffectView.Frame = window.Frame; blurEffectView.Tag = 808080; window?.AddSubview(blurEffectView); base.OnResignActivation(uiApplication); }
This will add our blurred view whenever they leave. Now to remove it when they come back, override the OnActivated
method:
public override void OnActivated(UIApplication uiApplication) { var window = UIApplication.SharedApplication.KeyWindow; window?.ViewWithTag(808080)?.RemoveFromSuperview(); base.OnActivated(uiApplication); }
And that’s it!
Bonus swift version: Override applicationWillResignActive
and applicationDidBecomeActive
in your AppDelegate.swift
.
func applicationWillResignActive(application: UIApplication) { let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = window!.frame blurEffectView.tag = 808080 self.window?.addSubview(blurEffectView) } func applicationDidBecomeActive (application: UIApplication) { self.window?.viewWithTag(808080)?.removeFromSuperview() }
But what is the system alerts are popped up, like location/push/camera permisson alerts. When this system alerts pops, even the resignactive method is called. So how can we avoid hiding the app in switcher?
LikeLike
GREAT question – you can keep some sort of “IsAlertShowing” flag to check if one is open. Just set that flag to true before firing any of the system alerts and check for it before firing the blur logic in the ResignActive method
LikeLike