Hazem Saleh's Blog, page 4

March 30, 2015

Back from JavaLand Germany 2015

JavaLand2015Main


I just get back from JavaLand that was be held from 24 March to 26 March @Brühl, Germany. The conference organization was more than fantastic and there were a lot of attendees in the conference sessions.


I had the chance to present “Developing Mobile Applications using JavaScript” in 25 March, the session had many attendees and I was really amazed by the energy, enthusiasm, and responsiveness of the attendees during the session.

screenshot


The session went great and I was glad to give a free copy of my JavaScript Mobile Application Development book to one of the attendees who answered a JavaScript quiz at the end of my session.

Books


I uploaded my session below:


[JavaLand 2015] Developing JavaScript Mobile Apps Using Apache Cordova from Hazem Saleh

In the conference, I really had the pleasure to work in the IBM Bluemix booth, it was really a very exciting experience for me.

IMG_8443


javaland5

IMG_8419


Finally, I would like to thank all the organizers of JavaLand conference for making the conference looks so great.

IMG_8429_2

IMG_8444

IMG_8372_2

1 like ·   •  2 comments  •  flag
Share on Twitter
Published on March 30, 2015 08:47

March 21, 2015

Speaking in ApacheCon North America 2015

Screen Shot 2015-03-21 at 1.15.10 AM

@Monday, April 13 10:45 AM, I will be speaking in ApacheCon North America conference (that will be held from 13 April to 17 April @Austin, USA) about Apache Cordova under the session title “Apache Cordova In Action”. My session will be a practical one, it will discuss why there is a need for Hybrid mobile development, the current challenges of mobile development, and how using Apache Cordova can help in overcoming many of these technical challenges. It also highlights the best practices of using Apache Cordova with jQuery mobile. then, it demonstrates a real Cordova mobile app that works on three mobile platforms (Android, iOS, and Windows Phone 8.1).


Finally, I hope it will be an interesting session for all the mobile development passionate :):

https://apacheconna2015.sched.org/speaker/hazems


Personally, it is my first time to visit Austin, beside enjoying technical stuff, I would like to visit some tourist places there, any suggestions are welcome :)?


I really wish to see all of you there in ApacheCon North America 2015!

 •  0 comments  •  flag
Share on Twitter
Published on March 21, 2015 04:32

March 5, 2015

Book Review #3 “Very good introduction into Apache Cordova”

Attached below the review of Werner Punz (a Web and Mobile Development Expert) about the “JavaScript Mobile Application Development” book:

JavaScript Mobile Application Development Book

JavaScript Mobile Application Development Book



Very good introduction into Apache Cordova


“This book is a very good introduction into Apache Cordova. It basically guides you from the basics to the API integration, then to the most common APIs. After that you get an overview on how to unit test the application and how to write your own plugins and in the end you will get a guide to the implementation of a complex Cordova app.

The only thing I personally found missing was in the IDE section a description on how to integrated Cordova in the Android Studio instead of Eclipse, since Eclipse is on its way out in the Android area of programming.”





Reference:

http://www.amazon.com/review/R19ORB0DCN16PA/


The book in Amazon:

http://www.amazon.com/JavaScript-Mobile-Application-Development-Hazem/dp/1783554177/

 •  0 comments  •  flag
Share on Twitter
Published on March 05, 2015 14:24

March 3, 2015

[JavaScript] Getting All Possible Permutations

One of the most interesting mathematical stuff is Permutation. A permutation is the act of re-arranging all the members of a set into some sequence or order, such that the order of selection always matters (unlike combination).


Assume that we have 3 balls (Red, Green and Blue). If we want all the possible permutation then we will have the following 6 possible permutation:



Red, Green, Blue.
Red, Blue, Green.
Green, Blue, Red.
Green, Red, Blue.
Blue, Red, Green.
Blue, Green, Red.

Mathematically, the number of permutations of n distinct objects is n factorial usually written as n!. Now, let’s write a simple function in JavaScript that gets the unique permutation for a set of objects.



var Util = function() {
};

Util.getPermuts = function(array, start, output) {
if (start >= array.length) {
var arr = array.slice(0); //clone the array
output.push(arr);
} else {
var i;

for (i = start; i < array.length; ++i) {
Util.swap(array, start, i);
Util.getPermuts(array, start + 1, output);
Util.swap(array, start, i);
}
}
}

Util.getAllPossiblePermuts = function(array, output) {
Util.getPermuts(array, 0, output);
}

Util.swap = function(array, from, to) {
var tmp = array[from];
array[from] = array[to];
array[to] = tmp;
}

// Test API ...
var array = ['R', 'G', 'B'];
var output = [];

Util.getAllPossiblePermuts(array, output);
console.log(output);

As shown in Util.getPermuts, it takes three parameters as follows:

1. array parameter represents the array of objects that we have.

2. start parameter represents the current start index.

3. output parameter represents the array that holds all the possible arrays of permutations.


Util.getPermuts recursively swaps the array elements in order to get all the possible permutations of the input array.


The previous code covers permutation without repetition which means that we use every element that we have only once in every possible permutation.


What about if we want to get all the possible permutations with repetition. Assume that we have 4 blank papers and we would like to paint them with all the possible ways using Red, Green and Blue colors.


Can you write a JavaScript function that can get all the possible 4 papers’ paintings?


According to Permutation with repetition, all the possible 4 papers’ paintings with 3 colors can be calculated as (3 power 4) which equal to 81. The formula is very simple: n P(with repetition) r = n ^ k.


Now, let’s use recursion in order to get all the possible permutation with repetition.



var Util = function() {
};

Util.getRPermuts = function(array, size, initialStuff, output) {
if (initialStuff.length >= size) {
output.push(initialStuff);
} else {
var i;

for (i = 0; i < array.length; ++i) {
Util.getRPermuts(array, size, initialStuff.concat(array[i]), output);
}
}
}

Util.getAllPossibleRPermuts = function(array, size, output) {
Util.getRPermuts(array, size, [], output);
}

As shown in Util.getRPermuts, it takes four parameters as follows:

1. array parameter represents the array of objects (colors in our case) that we have.

2. size parameter represents the size of every permutation item.

3. initialStuff parameter represents a temp array that holds every possible permutation with repetition.

4. output parameter represents the array that holds all the possible arrays of permutation with repetition.


In order to know all the possible 4 papers’ paintings using the available 3 colors, you can call the permutation with repetition API simply as follows:



// Create the array of the possible 3 colors ...
var possibleColors = ['R', 'G', 'B'];
var output = [];
var papersNo = 4;

// get all the possible painting for the 4 papers that we have.
Util.getAllPossibleRPermuts(possibleColors, papersNo, output);
console.log(output);

In the console, you will find all the possible 81 permutation with repetition as follows:



[["R", "R", "R", "R"], ["R", "R", "R", "G"], ["R", "R", "R", "B"], ["R", "R", "G", "R"], ["R", "R", "G", "G"], ["R", "R", "G", "B"], ["R", "R", "B", "R"], ["R", "R", "B", "G"], ["R", "R", "B", "B"], ["R", "G", "R", "R"], ["R", "G", "R", "G"], ["R", "G", "R", "B"], ["R", "G", "G", "R"], ["R", "G", "G", "G"], ["R", "G", "G", "B"], ["R", "G", "B", "R"], ["R", "G", "B", "G"], ["R", "G", "B", "B"], ["R", "B", "R", "R"], ["R", "B", "R", "G"], ["R", "B", "R", "B"], ["R", "B", "G", "R"], ["R", "B", "G", "G"], ["R", "B", "G", "B"], ["R", "B", "B", "R"], ["R", "B", "B", "G"], ["R", "B", "B", "B"], ["G", "R", "R", "R"], ["G", "R", "R", "G"], ["G", "R", "R", "B"], ["G", "R", "G", "R"], ["G", "R", "G", "G"], ["G", "R", "G", "B"], ["G", "R", "B", "R"], ["G", "R", "B", "G"], ["G", "R", "B", "B"], ["G", "G", "R", "R"], ["G", "G", "R", "G"], ["G", "G", "R", "B"], ["G", "G", "G", "R"], ["G", "G", "G", "G"], ["G", "G", "G", "B"], ["G", "G", "B", "R"], ["G", "G", "B", "G"], ["G", "G", "B", "B"], ["G", "B", "R", "R"], ["G", "B", "R", "G"], ["G", "B", "R", "B"], ["G", "B", "G", "R"], ["G", "B", "G", "G"], ["G", "B", "G", "B"], ["G", "B", "B", "R"], ["G", "B", "B", "G"], ["G", "B", "B", "B"], ["B", "R", "R", "R"], ["B", "R", "R", "G"], ["B", "R", "R", "B"], ["B", "R", "G", "R"], ["B", "R", "G", "G"], ["B", "R", "G", "B"], ["B", "R", "B", "R"], ["B", "R", "B", "G"], ["B", "R", "B", "B"], ["B", "G", "R", "R"], ["B", "G", "R", "G"], ["B", "G", "R", "B"], ["B", "G", "G", "R"], ["B", "G", "G", "G"], ["B", "G", "G", "B"], ["B", "G", "B", "R"], ["B", "G", "B", "G"], ["B", "G", "B", "B"], ["B", "B", "R", "R"], ["B", "B", "R", "G"], ["B", "B", "R", "B"], ["B", "B", "G", "R"], ["B", "B", "G", "G"], ["B", "B", "G", "B"], ["B", "B", "B", "R"], ["B", "B", "B", "G"], ["B", "B", "B", "B"]]
 •  0 comments  •  flag
Share on Twitter
Published on March 03, 2015 08:50

February 20, 2015

Speaking in JavaLand Germany 2015

Speaker in JavaLand 2015

Speaker in JavaLand 2015


@Wednesday, March 25 04:00 PM, I will be speaking in JavaLand Germany conference (that will be held from 24 March to 26 March @Brühl, Germany) about “Developing Mobile Applications using JavaScript”. My session will be a practical one, I will discuss mobile apps development using JavaScript and Apache Cordova. My session will also include Apache Cordova Integration tips with jQuery mobile on the three popular mobile platforms (Android, iOS and Windows Phone 8).


There is a JavaScript quiz at the end of my session, the one who will be able to answer it correctly will have a free copy of my new JavaScript book:

Books


I hope it will be an interesting session for all the mobile development passionate :):

https://www.doag.org/konferenz/konferenzplaner/konferenzplaner_details.php?locS=0&id=483801&vid=491546


Personally, it is my first time to visit Brühl, Germany, beside enjoying technical stuff, I would like to visit some tourist places there, any suggestions are welcome :D?


I really wish to see all of you there in JavaLand Germany 2015!

 •  0 comments  •  flag
Share on Twitter
Published on February 20, 2015 07:05

February 16, 2015

[JavaScript] Getting All Possible Combinations

One of the most interesting mathematical stuff is Combination. A combination is a way of selecting members from a grouping, such that the order of selection does not matter (unlike permutation).


For example, assume that we have 6 balls (numbered from 1 to 6), and a person can select only 4 balls at a time.


Can you write a JavaScript function that can get all the possible 4 ball selections from the available 6 balls?


According to Combination, all the possible 4 ball selections from the 6 balls are (6 Choose 4) which equal to 15. The formula is very simple: n C r = n! / r! n-r!.


Now, let’s use recursion in order to get all the possible combinations.



var Util = function() {
};

Util.getCombinations = function(array, size, start, initialStuff, output) {
if (initialStuff.length >= size) {
output.push(initialStuff);
} else {
var i;

for (i = start; i < array.length; ++i) {
Util.getCombinations(array, size, i + 1, initialStuff.concat(array[i]), output);
}
}
}

Util.getAllPossibleCombinations = function(array, size, output) {
Util.getCombinations(array, size, 0, [], output);
}

As shown in Util.getAllPossibleCombinations, it takes five parameters as follows:

1. array parameter represents the array of objects that we have.

2. size parameter represents the size of every selection from the array.

3. start parameter represents the current start index.

4. initialStuff parameter represents a temp array that holds every possible combination.

5. output parameter represents the array that holds all the possible arrays of combinations.


In order to know all the possible 4 balls selections from the available 6 balls, you can call the API simply as follows:



// Create an array that holds numbers from 1 ... 6.
var array = [];

for (var i = 1; i

In the console, you will find all the possible 15 combinations as follows:



[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 4, 5], [1, 2, 4, 6], [1, 2, 5, 6], [1, 3, 4, 5], [1, 3, 4, 6], [1, 3, 5, 6], [1, 4, 5, 6], [2, 3, 4, 5], [2, 3, 4, 6], [2, 3, 5, 6], [2, 4, 5, 6], [3, 4, 5, 6]]
 •  0 comments  •  flag
Share on Twitter
Published on February 16, 2015 12:36

January 21, 2015

Cool mobile apps for developers

In the past, only desktops or laptops could be used to create computer programs. However, thanks to technological advancements, today’s smartphones can now be used in the creation of software.


Mobile apps that can be used to create programs on the go were made in order to hasten the development process of apps, which is quite possibly the most lucrative business today. In a study featured on entertainment site Pocket Fruity, it was discussed that more and more people take their smartgadgets wherever they go, may it be at a train station, on a bus, or in a pub. Since the app-making industry is extremely cutthroat, developers created an edge in the form of apps in order to beat deadlines even when they’re not at their workstations.


Here are two useful mobile apps for the web devs out there.


The C Programming Language App

This is one of the best apps for the C programmers out there. It is a handy app for scribbling down codes as well as checking codes thanks to its Reference Tab. The reference tab has links to the language reference, string library, math library, and standard library.


The C Programming Language has a feature that allows developers to test whether or not a program will run smoothly on a chosen platform. It also allows users to easily send their codes via email once they’re done. The app can be used for free by iOS users.


JavaScript Anywhere

JavaScript Anywhere is a three-in-one app that acts as a Java Script, CSS, and HTML editor. It has a very simply user interface so developers can get the hang of using it quickly. Like the C Programming Language App, JavaScript Anywhere allows users to check whether or not their program will work smoothly. Users can import saved projects via the Internet and even store their work on Dropbox for backup. Jsany can also be downloaded for free by iOS users.

 •  0 comments  •  flag
Share on Twitter
Published on January 21, 2015 15:32

January 8, 2015

iPhone Login Form Implementation Tips using Swift

One of the most common scenarios is to implement login forms. In this post, I will show my thoughts regarding implementing a usable iPhone login form in iOS using Swift. Let’s check them one by one.


Moving up login elements when keyboard is present

One of the nice to have things is to move up the login elements when the keyboard is present after focusing on a text field as shown in the screenshot below.

1


In order to implement this behavior, you need to do the following:



Creating a Center Y alignment constraint on your login form and add it as an outlet as follows:

@IBOutlet var centerConstraint: NSLayoutConstraint!


Creating observers for "keyboardWillShow" and "keyboardWillHide" (that will be executed when the iPhone keyboard is shown or hidden):

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);


Implementing keyboardWillShow function to update the Center Y alignment constraint using the keyboard size as follows:

func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue();

UIView.animateWithDuration(0.1, animations: { () -> Void in
self.centerConstraint.constant = self.formCenterOriginal + (keyboardFrame.size.height / 2);
});
}

As shown above, you can get the keyboard frame from userInfo, and then the Center Y alignment constraint is updated with keyboardFrame.size.height / 2.
Implementing keyboardWillHide function to restore the Center Y alignment constraint’s original value as follows:

func keyboardWillHide(notification: NSNotification) {
self.centerConstraint.constant = self.formCenterOriginal;
}

Note that self.centerConstraint.constant is saved in formCenterOriginal attribute during viewDidLoad().



var formCenterOriginal: CGFloat = 0;

override func viewDidLoad() {
// ...

// Save the center constraint for using it later ...
self.formCenterOriginal = self.centerConstraint.constant;
}



Hiding keyboard when touching outside of text fields:

In order to hide the keyboard when touching outside of the form text fields, all what you need to do is to override touchesBegan function as follows.



override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true);
}

Calling view.endEditing(true) hides the keyboard.


Form Fields Navigation and Changing the text of “Done” button

It will be nice to change the text of the "Done" button based on the current text focus. This means that if the user name text field is focused then the "Done" button will be renamed to "Next". When the user clicks "Next" button, the password text field will be focused.


If the password text field is focused then the "Done" button will be renamed to "Go". When the user touches "Go" button, the keyboard will hide and the login logic will be executed.


In order to implement the form field navigation, we need to implement the UITextFieldDelegate protocol’s textFieldShouldReturn(theTextField: UITextField!) -> Bool function in our ViewController. Note that userNameText and passwordText delegate should be set to self during viewDidLoad().



override func viewDidLoad() {
// ...

// Override username and password events.
userNameText.delegate = self;
passwordText.delegate = self;
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {
if (textField == self.passwordText) {
textField.resignFirstResponder();

// call the login logic
login();
} else if (textField == self.userNameText) {
self.passwordText.becomeFirstResponder();
}

return true;
}

The previous code means that if the user name text field is focused and the user touches "Done" button then the password text field will be focused by calling self.passwordText.becomeFirstResponder(). if the password text field is focused and the user touches "Done" button then the keyboard be hidden by calling textField.resignFirstResponder() and after this we can call the login logic.


Note: both passwordText and userNameText are outlets for the text fields.


For changing the the text of "Done" button for passwordText and userNameText text fields, this can be done by changing the Return Key value in the text field properties as shown in the figure below.

Screen Shot 2015-01-08 at 11.30.01 PM


Implementing the login logic

Finally, we can implement the login logic which is simply mocked by an Alert message as shown below.



func login() {
let alertController = UIAlertController(title: "Welcome",
message: "Welcome" + ((userNameText.text == "") ? "!" : " " + userNameText.text + "!"),
preferredStyle: UIAlertControllerStyle.Alert);

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil));

self.presentViewController(alertController, animated: true, completion: nil);
}

If the user name is specified in the login form then it will be displayed in the Alert as shown in the figure below.

3


The following code shows the complete code of ViewController class.



import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
var formCenterOriginal: CGFloat = 0;

@IBOutlet var userNameText: UITextField!
@IBOutlet var passwordText: UITextField!
@IBOutlet var centerConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

// Create keyboard observers.
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);

self.formCenterOriginal = self.centerConstraint.constant;

// Override username and password events.
userNameText.delegate = self;
passwordText.delegate = self;
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func loginTouchDown(sender: AnyObject) {
login();
}

func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue();

UIView.animateWithDuration(0.1, animations: { () -> Void in
self.centerConstraint.constant = self.formCenterOriginal + (keyboardFrame.size.height / 2);
});
}

func keyboardWillHide(notification: NSNotification) {
self.centerConstraint.constant = self.formCenterOriginal;
}

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true);
}

func textFieldShouldReturn(theTextField: UITextField!) -> Bool {
if (theTextField == self.passwordText) {
theTextField.resignFirstResponder();
login();
} else if (theTextField == self.userNameText) {
self.passwordText.becomeFirstResponder();
}

return true;
}

func login() {
let alertController = UIAlertController(title: "Welcome", message: "Welcome" + ((userNameText.text == "") ? "!" : " " + userNameText.text + "!"), preferredStyle: UIAlertControllerStyle.Alert);

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil));

self.presentViewController(alertController, animated: true, completion: nil);
}
}

This is all about. If you have any questions or concerns, please feel free to comment.


Full source code

The whole project is available in the following GitHub URL:

https://github.com/hazems/swift-sample1

 •  0 comments  •  flag
Share on Twitter
Published on January 08, 2015 13:37

Simple iPhone Login Form Implementation Tips using Swift

One of the most common scenarios is to implement login forms. In this post, I will show my thoughts regarding implementing a usable iPhone login form in iOS using Swift. Let’s check them one by one.


Moving up login elements when keyboard is present

One of the nice to have things is to move up the login elements when the keyboard is present after focusing on a text field as shown in the screenshot below.

1


In order to implement this behavior, you need to do the following:



Creating a Center Y alignment constraint on your login form and add it as an outlet as follows:

@IBOutlet var centerConstraint: NSLayoutConstraint!


Creating observers for "keyboardWillShow" and "keyboardWillHide" (that will be executed when the iPhone keyboard is shown or hidden):

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);


Implementing keyboardWillShow function to update the Center Y alignment constraint using the keyboard size as follows:

func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue();

UIView.animateWithDuration(0.1, animations: { () -> Void in
self.centerConstraint.constant = self.formCenterOriginal + (keyboardFrame.size.height / 2);
});
}

As shown above, you can get the keyboard frame from userInfo, and then the Center Y alignment constraint is updated with keyboardFrame.size.height / 2.
Implementing keyboardWillHide function to restore the Center Y alignment constraint’s original value as follows:

func keyboardWillHide(notification: NSNotification) {
self.centerConstraint.constant = self.formCenterOriginal;
}

Note that self.centerConstraint.constant is saved in formCenterOriginal attribute during viewDidLoad().



var formCenterOriginal: CGFloat = 0;

override func viewDidLoad() {
// ...

// Save the center constraint for using it later ...
self.formCenterOriginal = self.centerConstraint.constant;
}



Hiding keyboard when touching outside of text fields:

In order to hide the keyboard when touching outside of the form text fields, all what you need to do is to override touchesBegan function as follows.



override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true);
}

Calling view.endEditing(true) hides the keyboard.


Form Fields Navigation and Changing the text of “Done” button

It will be nice to change the text of the "Done" button based on the current text focus. This means that if the user name text field is focused then the "Done" button will be renamed to "Next". When the user clicks "Next" button, the password text field will be focused.


If the password text field is focused then the "Done" button will be renamed to "Go". When the user touches "Go" button, the keyboard will hide and the login logic will be executed.


In order to implement the form field navigation, we need to implement the UITextFieldDelegate protocol’s textFieldShouldReturn(theTextField: UITextField!) -> Bool function in our ViewController. Note that userNameText and passwordText delegate should be set to self during viewDidLoad().



override func viewDidLoad() {
// ...

// Override username and password events.
userNameText.delegate = self;
passwordText.delegate = self;
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {
if (textField == self.passwordText) {
textField.resignFirstResponder();

// call the login logic
login();
} else if (textField == self.userNameText) {
self.passwordText.becomeFirstResponder();
}

return true;
}

The previous code means that if the user name text field is focused and the user touches "Done" button then the password text field will be focused by calling self.passwordText.becomeFirstResponder(). if the password text field is focused and the user touches "Done" button then the keyboard be hidden by calling textField.resignFirstResponder() and after this we can call the login logic.


Note: both passwordText and userNameText are outlets for the text fields.


For changing the the text of "Done" button for passwordText and userNameText text fields, this can be done by changing the Return Key value in the text field properties as shown in the figure below.

Screen Shot 2015-01-08 at 11.30.01 PM


Implementing the login logic

Finally, we can implement the login logic which is simply mocked by an Alert message as shown below.



func login() {
let alertController = UIAlertController(title: "Welcome",
message: "Welcome" + ((userNameText.text == "") ? "!" : " " + userNameText.text + "!"),
preferredStyle: UIAlertControllerStyle.Alert);

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil));

self.presentViewController(alertController, animated: true, completion: nil);
}

If the user name is specified in the login form then it will be displayed in the Alert as shown in the figure below.

3


The following code shows the complete code of ViewController class.



import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
var formCenterOriginal: CGFloat = 0;

@IBOutlet var userNameText: UITextField!
@IBOutlet var passwordText: UITextField!
@IBOutlet var centerConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

// Create keyboard observers.
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);

self.formCenterOriginal = self.centerConstraint.constant;

// Override username and password events.
userNameText.delegate = self;
passwordText.delegate = self;
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func loginTouchDown(sender: AnyObject) {
login();
}

func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue();

UIView.animateWithDuration(0.1, animations: { () -> Void in
self.centerConstraint.constant = self.formCenterOriginal + (keyboardFrame.size.height / 2);
});
}

func keyboardWillHide(notification: NSNotification) {
self.centerConstraint.constant = self.formCenterOriginal;
}

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true);
}

func textFieldShouldReturn(theTextField: UITextField!) -> Bool {
if (theTextField == self.passwordText) {
theTextField.resignFirstResponder();
login();
} else if (theTextField == self.userNameText) {
self.passwordText.becomeFirstResponder();
}

return true;
}

func login() {
let alertController = UIAlertController(title: "Welcome", message: "Welcome" + ((userNameText.text == "") ? "!" : " " + userNameText.text + "!"), preferredStyle: UIAlertControllerStyle.Alert);

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil));

self.presentViewController(alertController, animated: true, completion: nil);
}
}

This is all about. If you have any questions or concerns, please feel free to comment.


Full source code

The whole project is available in the following GitHub URL:

https://github.com/hazems/swift-sample1

 •  0 comments  •  flag
Share on Twitter
Published on January 08, 2015 13:37

January 5, 2015

Review #2 about the “JavaScript Mobile Application Development” Book

Attached below the review of Safwat about the “JavaScript Mobile Application Development” book:

JavaScript Mobile Application Development Book

JavaScript Mobile Application Development Book








Excellent


“The book is an easy and comprehensive guide to whoever is interested in multi platform mobile development with examples it shows the full life cycle of developing mobile cordova apps.”









Reference:

http://www.amazon.com/review/R2N5YBCWJK44V2/


The book in Amazon:

http://www.amazon.com/JavaScript-Mobile-Application-Development-Hazem/dp/1783554177/

 •  0 comments  •  flag
Share on Twitter
Published on January 05, 2015 07:13