Skip to content

Instantly share code, notes, and snippets.

View sag333ar's full-sized avatar

sagar kothari sag333ar

View GitHub Profile
@sag333ar
sag333ar / SessionDelegate.m
Last active December 28, 2015 09:59
NSURLSession, NSURLSessionConfiguration, NSURLSessionDelegate, NSURLSessionDataTask, AFNetworking, AFHTTPRequestOperation, Core-Class
#define IS_IOS_7 ([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7)
#define API_RQST_TIME_OUT 30
// part of header file --------------------------------------------------------
#import <Foundation/Foundation.h>
@protocol API_Handler <NSObject>
@required
- (void)didSuccess:(NSData*)data;
- (void)didFail:(NSError*)error;
@end
@sag333ar
sag333ar / detectDeviceType.m
Last active December 28, 2015 09:49
Detect device type of iPhone
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
typedef NS_ENUM(NSInteger, DeviceType) {
DeviceType_iPhone3Gs,
DeviceType_iPhone4_4s,
DeviceType_iPhone5,
};
- (DeviceType)getTypeOfDevice {
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
@sag333ar
sag333ar / getUDID.m
Last active December 28, 2015 09:49
Get the UDID of the current iOS device.
- (NSString*)UDID {
NSString *uuidString = nil;
// get os version
NSUInteger currentOSVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
if(currentOSVersion <= 5) {
if([[NSUserDefaults standardUserDefaults] valueForKey:@"udid"]) {
uuidString = [[NSUserDefaults standardDefaults] valueForKey:@"udid"];
} else {
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
@sag333ar
sag333ar / lastWeek
Created November 15, 2013 10:07
Get date of last week
- (NSDate *)lastWeek {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month;
comp.year = comp.year;
comp.day = comp.day-7;
[comp setHour:0];
[comp setMinute:0];
@sag333ar
sag333ar / yesterday
Last active December 28, 2015 09:49
Returns date of yesterday.
-(NSDate *)yesterday {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month;
comp.year = comp.year;
comp.day = comp.day-1;
[comp setHour:0];
[comp setMinute:0];
@sag333ar
sag333ar / firstDateForLastMonth
Created November 15, 2013 10:05
Get the First date of previous month.
-(NSDate *)firstDateForLastMonth {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month-1;
comp.year = comp.year;
comp.day = 1;
[comp setHour:0];
[comp setMinute:0];
@sag333ar
sag333ar / remove24HoursToDate
Created November 15, 2013 10:04
Current date - 24 hours = Today 5 PM - 24 hours = Yesterdays 5 PM
- (NSDate*)remove24HoursToDate:(NSDate*)date {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month;
comp.year = comp.year;
comp.day = comp.day;
[comp setHour:0];
[comp setMinute:0];
[comp setSecond:0];
@sag333ar
sag333ar / add24HoursToDate
Created November 15, 2013 10:03
add 23:59:59 hours to - (assume today is 30-Jul-2013) example range yesterday = 29-Jul-2013 12:00:00 AM + "to" + 30-Jul-2013 23:59:59 PM
// add 23:59:59 hours to - (assume today is 30-Jul-2013) example range yesterday = 29-Jul-2013 12:00:00 AM + "to" + 30-Jul-2013 23:59:59 PM
- (NSDate*)add24HoursToDate:(NSDate*)date {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month;
comp.year = comp.year;
comp.day = comp.day;
[comp setHour:23];
[comp setMinute:59];
@sag333ar
sag333ar / currentDateMidnightTime
Created November 15, 2013 10:03
Get current Date mid-night time = 15-Feb-2013 00:00:00 So, You will get date with time 00:00:00 always.
- (NSDate*) currentDate_midnightTime {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month;
comp.year = comp.year;
comp.day = comp.day;
[comp setHour:0];
[comp setMinute:0];
@sag333ar
sag333ar / twoMonthspastDate
Created November 15, 2013 09:58
Current date - 2 months period = the date which you get after executing following function
- (NSDate*)twoMonthspastDate {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
comp.timeZone=[NSTimeZone defaultTimeZone];
comp.month = comp.month-2;
comp.year = comp.year;
comp.day = 1;
[comp setHour:0];
[comp setMinute:0];