Posted By

sag333ar on 11/18/10


Tagged

c iphone objective iOS4


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

sag333ar
FumarMata
Tintenklecks


Add events to Calendar in iPhone (iOS4.0)


 / Published in: Objective C
 

This code will work only for iOS4.0

  1. #import "EventTestViewController.h"
  2. #import <EventKit/EventKit.h>
  3.  
  4. @implementation EventTestViewController
  5.  
  6.  
  7.  
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10.  
  11. EKEventStore *eventStore = [[EKEventStore alloc] init];
  12.  
  13. EKEvent *event = [EKEvent eventWithEventStore:eventStore];
  14. event.title = @"EVENT TITLE";
  15.  
  16. event.startDate = [[NSDate alloc] init];
  17. event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
  18.  
  19. [event setCalendar:[eventStore defaultCalendarForNewEvents]];
  20. NSError *err;
  21. [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. -(IBAction)btnSyncCalendar:(id)sender{
  37. Class aMsgClass = NSClassFromString(@"EKEventStore");
  38. if(!aMsgClass) return;
  39. NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  40. [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  41. [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  42. for (NSString *strKey in [dForOpening_Timings allKeys]) {
  43. NSDictionary *d=[dForOpening_Timings valueForKey:strKey];
  44. // dayName dayOfMonth monthName timing dateForSync
  45. EKEventStore *eventStore = [[EKEventStore alloc] init];
  46.  
  47. EKEvent *event = [EKEvent eventWithEventStore:eventStore];
  48.  
  49.  
  50. NSString *strTitle=[NSString stringWithFormat:@"%@",lblname.text];
  51.  
  52.  
  53. event.title = strTitle;
  54.  
  55. NSArray *arStartEnd=[[d valueForKey:@"timing"] componentsSeparatedByString:@" to "];
  56.  
  57. NSString *strStart=[arStartEnd objectAtIndex:0];
  58. if([strStart hasSuffix:@"pm"]){
  59. strStart=[NSString stringWithFormat:@"%02i:00:00",([[[strStart componentsSeparatedByString:@"pm"] objectAtIndex:0] intValue]+12)];
  60. } else {
  61. strStart=[NSString stringWithFormat:@"%02i:00:00",([[[strStart componentsSeparatedByString:@"pm"] objectAtIndex:0] intValue])];
  62. }
  63.  
  64. NSString *strEnd=[arStartEnd objectAtIndex:1];
  65. if([strEnd hasSuffix:@"pm"]){
  66. strEnd=[NSString stringWithFormat:@"%02i:00:00",([[[strEnd componentsSeparatedByString:@"pm"] objectAtIndex:0] intValue]+12)];
  67. } else {
  68. strEnd=[NSString stringWithFormat:@"%02i:00:00",([[[strEnd componentsSeparatedByString:@"pm"] objectAtIndex:0] intValue])];
  69. }
  70. strStart=[NSString stringWithFormat:@"%@ %@",[d valueForKey:@"dateForSync"],strStart];
  71. strEnd=[NSString stringWithFormat:@"%@ %@",[d valueForKey:@"dateForSync"],strEnd];
  72.  
  73.  
  74. event.startDate = [dateFormat dateFromString:strStart];
  75. event.endDate = [dateFormat dateFromString:strEnd];
  76.  
  77. NSLog(@"\n\nTitle is %@\nStartDate is %@\nEnd date is %@.",event.title,event.startDate,event.endDate);
  78.  
  79. [event setCalendar:[eventStore defaultCalendarForNewEvents]];
  80. NSError *err;
  81. [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
  82. }
  83. [dateFormat release]; dateFormat=nil;
  84. }

Report this snippet  

You need to login to post a comment.