2012-10-26 40 views
14

HyThêm sự kiện vào lịch trong xcode iOS

Tôi có mã này để thêm Sự kiện vào lịch nhưng không thêm.

-(void)event 
{ 
    EKEventStore *eventStore = [[EKEventStore alloc] init]; 

    EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
    event.title  = @"Event"; 


    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init]; 
    [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 


    NSString *dateandtime =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",starttimestring]; 
    NSString *dateandtimeend =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",endtimestring]; 



    event.startDate = [tempFormatter dateFromString:dateandtime]; 
    event.endDate = [tempFormatter dateFromString:dateandtimeend]; 


    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]]; 
    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]]; 

    [event setCalendar:[eventStore defaultCalendarForNewEvents]]; 
    NSError *err; 
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 
} 

Từ XML tôi nhận được ngày và thời gian ở định dạng này:

datestring: 28.10.2012

starttimestring: 15:00

+0

chắc chắn rằng bạn 'startDate' và' endDate' là ngày hợp lệ trước khi lưu – Maulik

+0

Khi tôi NSlog dateandtime và dateandtimeend tôi có định dạng này 28.10.2012 15:00 giống như định dạng ngày – WildWorld

+0

in 'NSError' – Maulik

Trả lời

24

Bạn có trên iOS 6 mô phỏng hoặc trên thiết bị chạy iOS 6? Nếu vậy, bạn cần yêu cầu người dùng cho phép sử dụng cửa hàng sự kiện trước khi bạn có thể lưu các mục vào đó. Về cơ bản, nếu requestAccessToEntityType: completion: chọn có sẵn trên đối tượng lưu trữ sự kiện của bạn, bạn gọi phương thức đó và cung cấp một khối mã được thực hiện khi người dùng cấp quyền và sau đó bạn sẽ lưu sự kiện của mình vào đó khối.

Đầu tiên thêm khuôn khổ EventKit để dự án của bạn và đừng quên để bao gồm việc nhập khẩu:

#import <EventKit/EventKit.h>

Dưới đây là một đoạn mã mà tôi đã sử dụng mà làm việc cho tôi:

EKEventStore *eventStore = [[EKEventStore alloc] init]; 
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
{ 
    // the selector is available, so we must be on iOS 6 or newer 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (error) 
      { 
       // display error message here 
      } 
      else if (!granted) 
      { 
       // display access denied error message here 
      } 
      else 
      { 
       // access granted 
       // ***** do the important stuff here ***** 
      } 
     }); 
    }]; 
} 
else 
{ 
    // this code runs in iOS 4 or iOS 5 
    // ***** do the important stuff here ***** 
} 

[eventStore release]; 

Đây là bài đăng trên blog mà tôi đã làm về chủ đề này:

http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/

+0

cảm ơn bạn :) hiện tại nó hoạt động tốt. tôi sẽ lấy lại cho bạn asap tôi thử nghiệm nó trên iPhone. tôi có trên câu hỏi, những gì là khác vào cuối mã tốt mũi? tôi biết đầu tiên nếu người khác nếu và người nào khác. không chắc chắn về điều cuối cùng. – WildWorld

+0

Tôi đã chỉnh sửa mã ở trên để giải thích thêm về những gì đang diễn ra. Một phần khác sẽ chạy nếu ứng dụng đang chạy trên iOS 4 hoặc 5, như trên các phiên bản của hệ điều hành, bộ chọn bộ sự kiện sẽ không tồn tại và bạn vẫn muốn các công việc quan trọng của mình được thực hiện trong trường hợp đó. –

+0

Và tất nhiên nếu nó đang làm việc cho bạn, bạn luôn có thể nhấp vào dấu kiểm ở bên trái của câu trả lời để chấp nhận nó. ; D –

2

1) thêm Eventkit khuôn khổ và #import <EventKit/EventKit.h>

2) chức năng

-(void)syncWithCalendar { 
    EKEventStore *store = [EKEventStore new]; 
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
     if (!granted) { return; } 
     EKEvent *event = [EKEvent eventWithEventStore:store]; 
     event.title = @"Event Title Testing"; //give event title you want 
     event.startDate = [NSDate date]; 
     event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; 
     event.calendar = [store defaultCalendarForNewEvents]; 
     NSError *err = nil; 
     [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; 
    }]; 
} 

3) gọi

[self syncWithCalendar]; 
Các vấn đề liên quan