Some common but Useful iOS Methods….


+(void)commonAlert:(NSString *)title msg:(NSString *)msg{

    UIAlertView *_alert=[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil, nil];

    [_alert show];

}


 

2.For checking network connection

+(BOOL)DeviceOnline{

    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];

    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];

    NSLog(@”network status %ld”,networkStatus);

    if (networkStatus == NotReachable) {

        

        UIAlertView *_alert=[[UIAlertView alloc] initWithTitle:@”No Network Connection” message:@”There are no network connection..” delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil, nil];

        [_alert show];

        NSLog(@”There IS NO internet connection”);

        return FALSE;

        

    } else {

        NSLog(@”There IS internet connection”);

        return TRUE;

    }

}


 

3.For validating Email

+ (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.For set color using HTML  color code

+(UIColor*)colorWithHexString:(NSString*)hex

{

    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    

    // String should be 6 or 8 characters

    if ([cString length] < 6) return [UIColor grayColor];

    

    // strip 0X if it appears

    if ([cString hasPrefix:@”0X”]) cString = [cString substringFromIndex:2];

    

    if ([cString length] != 6) return  [UIColor grayColor];

    

    // Separate into r, g, b substrings

    NSRange range;

    range.location = 0;

    range.length = 2;

    NSString *rString = [cString substringWithRange:range];

    

    range.location = 2;

    NSString *gString = [cString substringWithRange:range];

    

    range.location = 4;

    NSString *bString = [cString substringWithRange:range];

    

    // Scan values

    unsigned int r, g, b;

    [[NSScanner scannerWithString:rString] scanHexInt:&r];

    [[NSScanner scannerWithString:gString] scanHexInt:&g];

    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    

    return [UIColor colorWithRed:((float) r / 255.0f)

                           green:((float) g / 255.0f)

                            blue:((float) b / 255.0f)

                           alpha:1.0f];

}


 

5.For validating alphabet.

+(BOOL) validateAlphabets: (NSString *)alpha

{

    NSString *abnRegex = @”[A-Za-z ]+”; // check for one or more occurrence of string you can also use * instead + for ignoring null value

    NSPredicate *abnTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, abnRegex];

    BOOL isValid = [abnTest evaluateWithObject:alpha];

    return isValid;

}


 

6.Get Height of label

-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject

{

/* this requires the maximum display size of the label */

     CGSize _maximumSize = CGSizeMake(280, 1000);

     CGSize _newSizeOfLabel;

  CGRect textRect = [_str boundingRectWithSize:_maximumSize

                                         options:NSStringDrawingUsesLineFragmentOrigin

                                      attributes:@{NSFontAttributeName:fontOfObject}

                                         context:nil];

    

    CGSize size = textRect.size;

    

    _newSizeOfLabel = [_str sizeWithFont:fontOfObject constrainedToSize:_maximumSize

                       

                           lineBreakMode:[UILabel new].lineBreakMode];

    

    //return _newSizeOfLabel.height;

    return size.height;

    

}


 

8.Check device version

+(BOOL)checkForDeviceVersion{

    NSString *version = [[UIDevice currentDevice] systemVersion];

    

    //BOOL isAtLeast6 = [version floatValue] >= 6.0;

    BOOL isAtLeast7 = [version floatValue] >= 7.0;

    return isAtLeast7;

    

}


 

9.check Screen Hetght

+(CGFloat)checkForSrceenHeight{

    CGRect screenBound = [[UIScreen mainScreen] bounds];

    CGSize screenSize = screenBound.size;

    //CGFloat screenWidth = screenSize.width;

    CGFloat screenHeight = screenSize.height;

    NSLog(@”screen height is  %f”,screenHeight);

    return screenHeight;

}

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: