Saturday, January 19, 2013

Login for Mono Touch.


So folks.  Looks like making good code still has some worth.  Ipad / Iphone app, with Login functionality now.  This code is placed in the AppDelegate.cs in Mono Touch for the interface on app start.


            RootElement re = new RootElement("Login");
            Section creds = new Section("Credentials");
            Section welcome = new Section("Welcome to Youth Impact!");
            UIImageView ie;
            if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) ie = new UIImageView(UIImage.FromBundle("resized.png"));
            else ie = new UIImageView(UIImage.FromBundle("small2.png"));
            welcome.Add(ie);
            login = new EntryElement("Login","Enter your Email", "");
            password = new EntryElement("Password","","",true);

            creds.Add(login);
            creds.Add(password);
            Section button = new Section();
            button.Add(new StringElement("Sign In",delegate { Validate();}));

            re.Add(welcome);
            re.Add(creds);
            re.Add(button);

            window = new UIWindow (UIScreen.MainScreen.Bounds);
            DialogViewController dialog = new DialogViewController(re);


            window.RootViewController = new DialogViewController(re);

I only put in a bit of logic for swapping the image out for IPhone / Ipad.  There is some code to do for when the app shifts Orientation.  As your properly sized image needs to be replaced with an image with the proper width / height due to the orientation change.

Using that if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone), should provide you the ability to handle that.. and maybe I'll just update this post later.

In the Validate method, I check the two Entries, one for password, and one for Login.. and use my web service call to validate the user.. if the response comes back as valid credentials, then I load the default ViewController that was generated when you create a "Universal" iphone / ipad app in Mono Touch.

Most of this code was taken as ill-gotten gains from this great recipe on Xamarin:

Xamarin Login Window Recipe.


No comments:

Post a Comment