Thursday 16 October 2014

Register For Push Notification in iOS8

Register for Notification

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
 {  
 #ifdef __IPHONE_8_0  
   UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge  
                                              |UIRemoteNotificationTypeSound  
                                              |UIRemoteNotificationTypeAlert) categories:nil];  
   [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
 #else  
   UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;  
   [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];  
 #endif  
   return YES;  
 }  

Delegate Methods for Handle Push Notification


#pragma mark - Push Notification Methods

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  
 {  
   //register to receive notifications  
   [application registerForRemoteNotifications];  
 }

  
 //For interactive notification only  
 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler  
 {  
   //handle the actions  
   if ([identifier isEqualToString:@"declineAction"]){  
   }  
   else if ([identifier isEqualToString:@"answerAction"]){  
   }  
 }

  
 -(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken  
 {  
   NSLog(@“Device token : %@", deviceToken);  
 }

  
 - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error  
 {  
   NSLog(@"Failed to get token, error: %@", error);  
 }

  
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
 {  
   NSLog(@“Your notification Info : %@", userInfo);  
 }

  
 -(void)handleRemoteNitification:(UIApplication *)application userInfo:(NSDictionary *)userInfo  
 {  
   NSLog(@“Your notification Info : %@", userInfo);  
 }  

Tuesday 1 July 2014

useful macro function for objective-c


for get log with class name, method name and line no

 #define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)  

for get UIColor

 #define COLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]  

is device type iPad or iPhone

 #define IS_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  
 #define IS_iPhone ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)  

is system iOS7 Or less

 #define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)  

is device have 4inches display

 #define IS_4INCHES_DEVICE( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )  

for comparing system version

 #define SYSTEM_VERSION_EQUAL_TO(v)         ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  
 #define SYSTEM_VERSION_GREATER_THAN(v)       ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)  
 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  
 #define SYSTEM_VERSION_LESS_THAN(v)         ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)  
 #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)   ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)  

Friday 27 June 2014

Utility methods


Remove white space from string

 -(NSString*)trimString:(NSString *)theString  
 {  
      NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];  
      return theStringTrimmed;  
 }  


get cache directory path

 -(NSString *)applicationCacheDirectoryString  
 {  
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
   NSString *cacheDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  
   return cacheDirectory;  
 }  

for email validation

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

string to date conversion

 - (NSDate *)stringToDate:(NSString *)dateString withFormate:(NSString *)format  
 {  
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
   [dateFormatter setDateFormat:format];  
   NSDate *date = [dateFormatter dateFromString: dateString];  
   return date;  
 }  

date to string conversion

 -(NSString *)DateToString:(NSDate *)date withFormate:(NSString *)format  
 {  
   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
   [formatter setDateFormat:format];//2013-07-15:10:00:00  
   NSString * strdate = [formatter stringFromDate:date];  
   return strdate;  
 }  

get the age from birthdate

 -(NSString *)getAge:(NSDate *)birthDate  
 {  
   NSDate *dateA=birthDate;  
   NSDate *dateB=[NSDate date];  
   NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
   NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit  
                         fromDate:dateA  
                          toDate:dateB  
                         options:0];  
   NSLog(@"Difference in date components: %i/%i/%i", components.day, components.month, components.year);  
   return [NSString stringWithFormat:@"%i",components.year];  
 }  

get Image of view with its all subviews

 -(UIImage *)captureView:(UIView *)view  
 {  
   CGSize imageSize = CGSizeZero;    
   imageSize=CGSizeMake(view.bounds.size.width, view.bounds.size.height);  
   UIGraphicsBeginImageContext(imageSize);  
   CGContextRef context = UIGraphicsGetCurrentContext();  
   [view.layer renderInContext:context];  
   UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();  
   UIGraphicsEndImageContext();  
   return screenShot;  
 }