iPhone Development: Some tips and tricks for iPhone development

In this post, I am going to tell you about some of the common things that need to be used in each project like validate a text field, show alert view, show HUD etc. Instead of writing code of lines again and again for showing alert and validate text fields, check for internet we can wrote the in our AppDelegate file as class methods(the methods that start with + sign are known as class methods as they can be called by using class name and we did not had to made any instance for calling these methods).

The few Utilities methods are shown below

1. Show alert View with tittle and message
Below given method will display an UIAlertView with tittle and message as passed to it at run-time.
+(void)showAlertWithTittle:(NSString *)tittleString andMessage:(NSString *)messageString
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:tittleString message:messageString delegate:nil cancelButtonTitle:@”Ok” otherButtonTitles: nil];
    [alert show];
 [alert release];
}

2. Check for empty field validation
Below given method will check for an empty text field at run-time.

+(BOOL)chkForEmptyField:(UITextField *)textField {
    BOOL chk = FALSE;
    if (textField.text.length==0) {
        chk=TRUE;
    }
    return chk;
}

3. Check for valid email id
Below given method will validate for email entered by user at run-time.

+(BOOL)validateEmailWithString:(NSString*)email
{
    NSString *emailRegex = @”[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}”;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex];
    return [emailTest evaluateWithObject:email];
}

4. Check for internet connection 
Below given method will check internet connection availability at run-time. 

+(BOOL)CheckForInternet
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if (internetStatus == NotReachable) {
        UIAlertView *networkAlert = [[UIAlertView alloc]initWithTitle:@”Sorry!” message:@”Unable to connect with servernPlease try again later….” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
        [networkAlert show];
        [networkAlert release];
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

NOTE:- Get Rechability file from here, and import system-configuration framework FILES
For more details check this article Check for internet connection 

5. Show HUD for a view with tittle and message
Below given method will display an hud, you can use it while loading some data from server or doing an heavy process at run-time. 
+(void)showHUDInView:(UIView *)view withTittle:(NSString *)tittle andHideCheck:(BOOL)hideCheck
{
    MBProgressHUD *hud;
    if (!hideCheck)
    {
        hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.labelText = tittle;
    }
    else
    {
        [MBProgressHUD hideHUDForView:view animated:YES];
    }

}

NOTE: You can download HUD Files From here HUD FILES

To use above methods in your class, just import your Appdelegate.h in .pch(pre compile header file) file, so that you don’t need to import it again and again.You can use above methods as shown below
if ([AppDelegate chkForEmptyField:userNameTextField])
    {
        [AppDelegate showAlertWithTittle:@”Oops!” andMessage:@”Please enter username.”];
    }
    else {
        if ([AppDelegate chkForEmptyField:passwordTextField]) {
            [AppDelegate showAlertWithTittle:@”Oops!” andMessage:@”Please enter password.”];
        } else {
            if ([AppDelegate CheckForInternet]) {
 [AppDelegate showHUDInView:self.view withTittle:@”Authenticating with App…” andHideCheck:FALSE];
   }
        }
    }