ModalViewController (loginView) – iTrade

Most application would like the trader to log in before performing any action.  In the iTrade app I would like the trader to log in first and then proceed with trading.  For the trader to log in first, its necessary that the login screen be displayed before any other view.  To achieve this I used “PresentModalView” property on the navigation controller.

I have been fighting with this since last night and here is an additional tip that I learnt on the way

Get the hell out of AppDelegate as soon as possible

If you are coming from windows forms development, you might think its as simple as display a login form and validating the credentials.  Although it indeed is as simple as that, the important point is where to call the PresentModalView controller.  Initially I thought I would have to present it in the AppDelegate.Finished Launching. But that’s not the case.  I need to add it to the RootViewController of the navigation controller.

Add a new Controller With A View to your solution and call it “LoginContoller”.  Add fields for user name and password, and add your logic for validation.  In my case, I am using a web service to validate the login (more on this later).  Ofcourse, set the connections to your buttons and text fields in InterfaceBuilder and in your code, add an event OnLogin.  I use this event to indicate to the RootViewContoller that Login is successful.  This is how my login controller looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using iTrade.TradingServices;

namespace iTrade
{
	public partial class LoginController : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need
		// to be able to be created from a xib rather than from managed code

		public LoginController (IntPtr handle) : base(handle)
		{
			Initialize ();

		}

		[Export("initWithCoder:")]
		public LoginController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public LoginController () : base("LoginController", null)
		{
			Initialize ();

		}

		void Initialize ()
		{
		}

		#endregion

		TradingService client;
		public event EventHandler OnLogonSuccessful;
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			btnLogin.TouchUpInside += HandleBtnLoginTouchUpInside;
			client = new TradingService();
		}

		void HandleBtnLoginTouchUpInside (object sender, EventArgs e)
		{
			activityIndicator.StartAnimating();
			client.BeginLogin("","", LoginCompletedEventHandler, true);

		}
		void LoginCompletedEventHandler(IAsyncResult result)
		{
			var answer = client.EndLogin(result);
			ApplicationData.IsLoggedIn = answer;
			InvokeOnMainThread( () =>
			                   {
				activityIndicator.StopAnimating();
				if(answer)
				{
					using(UIAlertView av = new UIAlertView("Logged IN", "You are now logged in ", null, "OK",null))
					{
						av.Show();
					}
					OnLogonSuccessful(this, new EventArgs());
				}
				else
				{
					using(UIAlertView av = new UIAlertView("Logged IN", "You are not logged in ", null, "OK",null))
					{
						av.Show();
					}
				}

			});

		}

	}
}

Back in your RootViewContoller, you need to create an object of LoginViewController and present it using the rootview’s navigation controller.  For the login view to be displayed correctly, you need to override the ViewWillLoad method in your RootViewController.  Create an object of type LoginController and delegate the OnLogin event to a method. This is where we will call PresentModalViewController

                void OnSuccessfulLogon (object sender, EventArgs e)
		{
			this.NavigationController.DismissModalViewControllerAnimated(true);
		}

		public override void ViewWillAppear (bool animated)
		{
			base.ViewWillAppear (animated);
			if (!ApplicationData.IsLoggedIn)
			{
				var lc = new LoginController();
				lc.OnLogonSuccessful += OnSuccessfulLogon;
				this.NavigationController.PresentModalViewController(lc, true);
			}
		}

Notice in the OnSuccessFulLogOn I dismiss the ModalViewController.

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: