Gerald Versluis: Sleeping Beauty – Making your iOS app sleeping beautifully

Sometimes you will find yourself creating an app which contains sensitive user information. I found myself there. Of course, you want to protect it the best you can. So you encrypt it, double salt it, encrypt it again, put it in a fortified building, set it up with Touch ID. Yet a small forgotten detail could leave you out in the cold.

A feature that is quickly overlooked, but can leak information nonetheless is the multitask screen on iOS. When you double press the home button, a handy overview screen comes up with which you can easily switch to another app. And while doing so it looks crazy sexy! But. To make it look so good, Apple takes a snapshot of your screen just before it gets send to the background, and that screen might just contain some information that you do not want to be seen.

This was a situation I found myself in for a recent project, and so I set out on a new adventure, making my app sleep like a beauty.

Although I am aware that Windows Phone and Android have similar functionality, the project was targeting only iOS, so my solution is limited to this OS only.
If you have done this for one of the other platforms please share it so I can learn from you!

Good night!

Actually there are a few ways to do it;

  • Hide the content of the current screen altogether;
  • Give it a nice blur of sorts;
  • Show a static image.

And probably some more I didn’t think of. Basically you can create any kind of view as we will see later on.

While we found this to be important to our project, it (of course) could not take up to much of our time. For this we choose the easiest, yet good looking solution and would just show a static image. Which also happens to be the splashscreen! Talk about multitasking!

So how to go about it.

First, we have to tell iOS to stop taking screenshots of our app when we put it to sleep. For this we use the IgnoreSnapshotOnNextApplicationLaunch method.

Then there are two events that are of importance in your AppDelegate; the OnResignActivation and OnActivated. These simply do as one might expect: do stuff when the app is going to the background and when it is activated again. Actually in retrospect there might be more suited events because these also fire when you slide down the notification center or swipe up to use the command center which is a bit overkill. But hey, better safe than sorry!

Now that we have identified all of our ingredients let’s put them together. First lets dissect the part where we sent our app to the background.

// Obscure UI in multitasking view
   public override void OnResignActivation(UIApplication uiApplication)

      // Prevent taking snapshot

      // Create our hiding view which is transparent initially. Also note the tag I'm giving the view. Why 42 you ask..?
      var bgView = new UIImageView(UIImage.FromFile("Default-Landscape.png")) { Tag = 42, Alpha = 0 };

      // Add the view to our current window

      // Animate it to the front and thus hide the contents of the app
      UIView.Animate(0.5, () =>
         bgView.Alpha = 1;

If added comments to the code so it should be self-explanatory. The only part that probably needs some extra attention is why I am giving the view a tag.
This is so we can later find the view by this tag to hide it again. Again, in retrospect there are probably more elegant ways to go about this. You probably want to declare a private variable in which you create the hiding view. I haven’t tried if that works, so please let me know if you have any improvements!
O and 42 can of course be anything, as long as it is the same in our OnActivated event.

Also, as I said, we have recycled the splashscreen for this. The app was a iPad app which would only be used in landscape mode, so this was the easiest way. If you have a app which can rotate you probably have to apply some of your own magic to determine the correct width and height of your hiding view, which is probably easiest to just get by copying it off the uiApplication.KeyWindow.Frame. Also see the example GitHub.

Like I have stated earlier; you could build any view you like here, whatever looks best to you and best suits your needs. Just keep in mind that it has to perform!

In the OnActivated event we do roughly the reverse.

// Bring back main interface from obscuring
   public override void OnActivated(UIApplication uiApplication)

        // Find our hiding view by the same tag we saw earlier
	var view = uiApplication.KeyWindow.ViewWithTag(42);

        // If we found it, hide it again!
	if (view != null)
           // Animate it back to transparent
	   UIView.Animate(0.5, () =>
	      view.Alpha = 0;
	   }, () =>
                // And after that completed, remove te view altogether

In this code I have added some comments as well so the working should be pretty obvious.

While running it looks like this.

Sleeping Beauty in action
Sleeping Beauty in action

All code can be found in a GitHub repository. It may slightly differ from the code and what I have described above because it is code from an actual app, which exposes potential private data so it would be a bit awkward if I would just post screenshots of it here

If you have any questions or remarks please let me know!


Jesse Liberty: Yet Another Podcast #145: John Sonmez

This week John Sonmez goes beyond programming to talk about life and the pursuit of happiness. Simple Programmer Blog: Soft Skills: The Software Developer’s Life Manual: 10 Steps to Learn Anything Quickly course: YouTube channel: Listen | Yet … Continue reading

Xamarin: Early Bird Prices for Xamarin Evolve 2016 End December 31!

Early Bird ticket prices (more than 20% off!) for Xamarin Evolve 2016 in Orlando, Florida will end at midnight on on December 31, 2015—register now so you don’t miss out!   The Future of Apps Xamarin Evolve 2016 includes two full days of technical training covering iOS, Android, and cross-platform best practices and is the […]

The post Early Bird Prices for Xamarin Evolve 2016 End December 31! appeared first on Xamarin Blog.


Adam J Wolf: Weekly Xamarin Newsletter Issue #72

Building Native, Cross-Platform Mobile Apps with Xamarin 4 Courtney Witmer, from Xamarin Inc., has released the Xamarin 4 webinar recording. Xamarin Link Roundup – 22 December 2015 Kym Phillpotts, from Xamarin Inc., has all your Xamarin links in one blog post. Xamarin: Learn Mobile Development Best Practices at Xamarin Evolve 2016 Joseph Hill, from Xamarin […]

The post Weekly Xamarin Newsletter Issue #72 appeared first on Syntax is my UI.


Xamarin: Webinar Recording: Building Native, Cross-Platform Mobile Apps with Xamarin 4

Xamarin has everything you need to build, test, and monitor great mobile apps, and with the release of Xamarin 4, building mission-critical mobile apps your users will love just got even easier. In this webinar recording, Developer Evangelist James Montemagno shows you how to start coding, debugging, and prototyping iOS, Android, and Windows apps on […]

The post Webinar Recording: Building Native, Cross-Platform Mobile Apps with Xamarin 4 appeared first on Xamarin Blog.