Kevin McMahon

Enthusiast

Blog Search

Monotouch Quickie: Network Indicator Tip

I’ve used this simple technique below many times in WinForms apps to toggle the wait cursor (hourglass) during long running operations and it is applicable for the situations that call for toggling the network activity indicator in Monotouch.When used with a using statement, the indicator is turned on when the object is constructed and turned off when the code within the using statement has finished executing and the object’s Dispose method is called.

public class NetworkIndicator : IDisposable{   public NetworkIndicator ()   {      UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;   }   public void Dispose ()   {      UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;            GC.SuppressFinalize(this);   }}

Usage:<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b22771bd-ad3c-4b68-a5ee-b2dcd3af7d42" style="margin: 0px; display: inline; float: none; padding: 0px;"><pre style="background-color: #ffffff; white-space: pre-wrap; word-wrap: break-word; overflow: auto;">using(new NetworkIndicator()){ //Make network call here}</pre></div>

You wouldn’t want to wrap this around a UIWebView (add the toggling of the indicator to the LoadStarted and LoadFinished events) but anytime your app is making calls out the network you can wrap the using statement around the method invocation and get the user friendly network activity notification.