Wandering Mobile

Over the past few weeks i have been working on a project which required not only a mapping element but also required custom map tiles to be overlaid on the map. This is actually a really simple process using Xamarin.Forms (I haven’t added Windows Phone here but i will edit this later) which requires only a basic knowledge of custom renderers.

Create a new Forms project and add the Xamarin.Forms.Maps nuget package to the core PCL, iOS and Android projects. In the Android project you also need to set the following settings in the AndroidManifest.xml:

Access to the Network State – Maps API v2 must be able to check if it can download the map tiles.

Internet Access – Internet access is necessary to download the map tiles and communicate with the Google Play Servers for API access.

Handoff is a feature in iOS 8 that really gets me excited, it is a feature that gives a user the feeling that something magical just happened. Without any effort on the users part state is passed from one device to another enabling them to carry on where they left off. If i did not know any better i would honestly believe this could only be attributed to pure magic. Well, we do know better so let’s get onto how we build this into our own Xamarin apps.

Project Setup

To begin we need to add a new entry into the info.plist, they key is NSUserActivityTypes with and array as the value. you can the add a reverse domain url for each activity type you wish to be shared between apps.

Sharing Data

In our view controller we are now able to setup our state data which can be shared to other devices when in range. To do this we use the NSUserActivity class which is available through the UserActivity property on all UIResponder elements which most things in your view hierarchy derive from. We create the UserActivity we instantiate it by passing in the activity type like so:

To set the state we wish to share we only have to override the UpdateUserActivityState method which again resides on UIResponder. This method is called periodically by the OS but can be requested by setting the NeedsSave flag to true on the UserActivity object. Ideally we should only update the state in this method as prior to being called the OS will clean out the UserInfo dictionary;

In our AppDelegate we have two touch points to consider, overriding WillContinueUserActivity gives us the chance to decide if we are able to continue the incoming activity. If this is something the app can handle we can show the user a loading spinner while the activity is populated.

Secondly we must override ContinueUserActiviy, this gives us access to the fully populated user activity object and a completion handler which accepts an array of NSObject. We use this completion handler to pass in all of the UI elements or view controllers wish to handle the activity which has been passed. These will then have RestoreUserActivityState called on them.

As you have probable seen by now, UIVisualEffectView in iOS 8 gives us an easy way to apply a blur or vibrancy effect to UI elements. While alone this effect is pretty cool, I wanted to see what we could do if we had a bit of fun with it.

Blurred MapView

The concept here is to force the focus on the location you wish the user to be looking. To do this we blur out the entire map but leave a circular transparent section in the centre. To do this we set up our view like this:

With the advent of iOS 8 and the inclusion of the UIPresentationController we now have a free reign in terms of presenting and transitioning to modal views in iOS.

To create a custom modal transition we are required to use 3 classes:

UIPresentationController

UIViewControllerAnimatedTransitioning

UIViewControllerAnimatedTransitioningDelegate

Apart from the excessively long names, these classes are really easy to use and setting up your awesome modal presentation transition will be a doddle.

UIPresentationController

This class is responsible for being the container of the view being presented, in my example we create a dimmed view to overlay the view being transitioned from. The required overriden methods / properties are:

FrameOfPresentedViewInContainerView: returns a frame which the presented view will be displayed in.

PresentationTransitionWillBegin: apply any visual configuration / animations to the container view before the presenting transition.

DissmissalTransitionWillBegin: apply any visual configuration / animations to the container view before the dismissing transition.

The glue that holds all of this awesomeness together, responsible for creating the UIPresentationController and marshalling the UIViewControllerAnimatedTransitioning instances for dismissal and presentation.

The second class brand new to iOS 8 is CMAltimeter which seems to point to the new hardware to be made available with the next iteration of the iPhone / iPad. The main function here is to get the relative altitude of the device but this is currently not supported on any available hardware.

I think just based on these 2 new classes we can tell the context is going to be huge moving forward, whether you think it is an invasion of privacy or an innovation that your mobile device knows how many flights of stair you have claimed that day is a question that only you can answer. All i can say is that as app developers we have lots of options moving forward.