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];
}
}
}