Search This Blog

Tuesday, 29 September 2015

iOS Generate Random string

+(NSString *) generateRandomString
{
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int len = 8;
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length])]];
    }
    
    return randomString;

}

Get iPhone application for portfolio or profile

Get iPhone application for your professional profile
Check my iPhone application below :
Easy to install just Click on below link and there will be install button tap on it finish !
Easy to share with your colleague or clients anyone 
No apple account required
Very Cheaper than you imagine 
If you want this application for your profile so contact:
a.kareem.tn@gmail.com

iOS NSDate -- All type of conversions Class Methods

//Get the current Device Date
+(NSDate *)getcurrentDate
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"yyyy-MM-dd h:mm a"];
    return CurrentDate;
}

//Get Month Year in String Format  From  Date
+(NSString *)getStringMonthYearFromDate :(NSDate *)givendate
{
    
    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMMM-y"];
    NSString *FormattedDate = [Formatter stringFromDate:givendate];
    
    return FormattedDate;
}

+(NSDate *)getcurrentMonthYear
{
    NSDate *CurrentDate = [NSDate date];
    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMMM y HH"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    
    NSDate *actualDate =[Formatter dateFromString:FormattedDate];
    
    NSDateComponents *components = [dateCalender components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour fromDate:actualDate];

    [components setCalendar:dateCalender];
    
    [components setDay:1];
    [components setHour:4];
    
    actualDate = [dateCalender dateFromComponents:components];
    return actualDate;
}

+(NSDate *)getcurrentDateOnly
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"dd-MMM-yyyy"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    return [Formatter dateFromString:FormattedDate];
}

+(NSString *)getcurrentDateInString
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"dd-MMM-yyyy HH:mm a"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    return FormattedDate;
}

+(NSDate *)getcurrentTime
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    return CurrentDate;
}

+(NSDate *)convertStringDateToNSDate :(NSString *)dateString
{

    NSCalendar *currentCalender =[NSCalendar currentCalendar];

    NSDateFormatter *stringToDateFormater =[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [stringToDateFormater setLocale:locale];
    [stringToDateFormater setDateFormat:@"dd MMMM y"];
    
    NSDate *fulldate = [stringToDateFormater dateFromString:dateString];
    
    NSDateComponents *components = [currentCalender components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour fromDate:fulldate];

    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    [components setCalendar:dateCalender];

    [components setDay:1];
    [components setHour:4];
    fulldate = [dateCalender dateFromComponents:components];

    return fulldate;
}

+(NSDate *)convertStringTimeToDate :(NSString *)time
{
    NSDate *CurrentDate = [NSDate date];
    NSCalendar *currentCalender =[NSCalendar currentCalendar];
    
    NSDateComponents *components = [currentCalender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:CurrentDate];
    
    NSDateFormatter *stringToDateFormater =[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [stringToDateFormater setLocale:locale];
    [stringToDateFormater setDateFormat:@"h:mm a"];
    
    NSDate *date = [stringToDateFormater dateFromString:time];
    
    NSCalendar *dateCalender =[NSCalendar currentCalendar];
    
    NSDateComponents *datecomponents = [dateCalender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
    
    [datecomponents setDay:components.day];
    [datecomponents setMonth:components.month];
    [datecomponents setYear:components.year];
    date = [dateCalender dateFromComponents:datecomponents];
    return date;
}

+(NSString *)convertTimeToString :(NSDate *)time
{
    NSDate *CurrentDate = time;
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    return timeString;
}

+(NSString *)convertDateToString :(NSDate *)date
{
    NSDate *CurrentDate = date;
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMM"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    return timeString;
}

+(NSString *)getcurrentTimeInString
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    
    
    return timeString;
}

+(NSString *)convertMinutesToTimeInterval :(NSUInteger)minutes
{
    NSTimeInterval timeInterval =minutes*60;
    NSDateComponentsFormatter *componentFormatter = [[NSDateComponentsFormatter alloc] init];
    
    componentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort;
    componentFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
    
    NSString *formattedString = [componentFormatter stringFromTimeInterval:timeInterval];
    return formattedString;
}

+(NSString *)convertMinutesToTimeIntervalNumericWay :(NSUInteger)minutes
{
    NSTimeInterval timeInterval =minutes*60;
    NSDateComponentsFormatter *componentFormatter = [[NSDateComponentsFormatter alloc] init];
    
    componentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
    componentFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
    
    NSString *formattedString = [componentFormatter stringFromTimeInterval:timeInterval];
    return formattedString;

}

Thursday, 20 November 2014

iOS Phone No Validation



-(BOOL)checkValidation :(NSString *)phoneNo
{
    NSString *phoneRegex = @"((971)\\d{9})";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    
    BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNo];
    return phoneValidates;
    

}

Thursday, 24 July 2014

Youtube Embeded not playing in landscape mode in iOS6 and above

Just Copy and paste below code in your AppDelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    id presentedViewController = [window.rootViewController presentedViewController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
    
    if (window && [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) {
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
    

}

Saturday, 12 July 2014

Play Youtube video in webview with HTML

Copy and Paste below code and load in your webview:

NSURL *myURL =[NSURL URLWithString:@"http://www.youtube.com/v/zah99ETVOz4"];

NSString *embedHTML = @"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"212\" height=\"172\"><param name=\"movie\" value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%@\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"270\"></embed></object></div></body></html>";

NSString *html = [NSString stringWithFormat:embedHTML, myURL,myURL,myURL];





[YourWebView loadHTMLString:html baseURL:nil];