ios - 将事件添加到 iOS 7 日历
<p><p>在我的应用程序中,我必须向 iOS 7 日历添加一个事件。我在 iOS 6 中制作了一个类似的应用程序,我使用了以下代码:</p>
<p><strong>CalendarUtility.h</strong></p>
<pre><code>#import <EventKit/EventKit.h>
#import <Foundation/Foundation.h>
@interface CalendarUtility : NSObject
- (void)setupEventForCalendarWithEvent:(EKEvent*)event andEventStore:(EKEventStore*)eventStore andCellDateTime:(NSString*) cellDateTime;
- (void)deleteEventFromCalendarWithCellID:(int)cellID;
+ (NSString*)getMonthStringFromMonthNumber:(NSString*)monthNumber;
+ (NSString*)getMonthNumberFromMonthString:(NSString*)monthString;
@end
</code></pre>
<p><strong>CalendarUtility.m</strong></p>
<pre><code>#import "CalendarUtility.h"
@interface CalendarUtility() {
NSMutableArray *arrayofEventId;
}
@end
@implementation CalendarUtility
- (id)init {
self = ;
if (self) {
arrayofEventId = [init];
}
return self;
}
- (void)setupEventForCalendarWithEvent:(EKEvent*)event andEventStore:(EKEventStore*)eventStore andCellDateTime:(NSString*) cellDateTime {
NSString *dayMonth = ;
NSString *month = ;
NSString *dateWithMonthNumber = ] withString:];
NSDateFormatter *dateFormat = [init];
;
NSDate *date = ;
event.startDate = date;
event.endDate = [ initWithTimeInterval:3600 sinceDate:date];
NSArray *arrAlarm = ];
event.alarms = arrAlarm;
];
NSError *err;
;
NSString *str = [ initWithFormat:@"%@", event.eventIdentifier];
;
}
- (void)deleteEventFromCalendarWithCellID:(int)cellID {
EKEventStore* store = [ init];
EKEvent* eventToRemove;
NSString *str;
if (cellID == 0) {
eventToRemove = ];
str = [ initWithFormat:@"%@", eventToRemove.eventIdentifier];
} else {
eventToRemove = ];
str = [ initWithFormat:@"%@", eventToRemove.eventIdentifier];
}
if (eventToRemove != nil) {
NSError* error = nil;
;
}
}
+ (NSString*)getMonthStringFromMonthNumber:(NSString*)monthNumber {
if () {
return @"Gen";
}
if () {
return @"Feb";
}
if () {
return @"Mar";
}
if () {
return @"Apr";
}
if () {
return @"Mag";
}
if () {
return @"Giu";
}
if () {
return @"Lug";
}
if () {
return @"Set";
}
if () {
return @"Ott";
}
if () {
return @"Nov";
}
if () {
return @"Gen";
}
if () {
return @"Dic";
}
return @"0";
}
+ (NSString*)getMonthNumberFromMonthString:(NSString*)monthString {
monthString = ;
if () {
return @"01";
}
if () {
return @"02";
}
if () {
return @"03";
}
if () {
return @"04";
}
if () {
return @"05";
}
if () {
return @"06";
}
if () {
return @"07";
}
if () {
return @"08";
}
if () {
return @"09";
}
if () {
return @"10";
}
if () {
return @"11";
}
if () {
return @"12";
}
return 0;
}
@end
</code></pre>
<p>在需要将事件添加到日历的 Controller 中,我使用了以下代码:</p>
<p><strong>向日历添加事件的方法</strong></p>
<pre><code>- (void) addEventToCalendar:(EpisodeCell*)cell andCell:(int)cellID {
if () {
EKEventStore *eventStore = [ init];
if ()
{
// 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)
{
NSString *errorMessage = ;
UIAlertView *alert = [ initWithTitle:@"InteracTV" message:errorMessage delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
}
else if (!granted)
{
UIAlertView *alert = [ initWithTitle:@"InteracTV" message:@"Impossibile accedere al calendario: controlla le impostazioni di privacy"delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
}
else
{
// access granted
EKEvent *event = ;
event.title = cellTitle;
if (cellID == 0) {
;
;
} else {
;
;
}
}
});
}];
}
else
{
// this code runs in iOS 4 or iOS 5
EKEvent *event = ;
event.title = cellTitle;
if (cellID == 0) {
;
;
} else {
;
;
}
}
// // PARTE PER iOS < 6 NON IMPLEMENTATO
// UIAlertView *alert = [ initWithTitle:@"Bitmama iTv" message:@"Aggiorna il dispositivo almeno ad iOS 6 se vuoi aggiungere un evento al calendario." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
// ;
} else {
;
;
}
}
- (void)animationForInsertedEventInCalendar {
; } completion:^(BOOL finished)
{
[UIView transitionWithView:self.imageViewOk
duration:0.5 options:UIViewAnimationOptionAllowAnimatedContent
animations:^(void){ ; } completion:nil];
}];
}
- (void)animationForDeletedEventFromCalendar {
; } completion:^(BOOL finished)
{
[UIView transitionWithView:self.imageViewDelete
duration:0.5 options:UIViewAnimationOptionAllowAnimatedContent
animations:^(void){ ; } completion:nil];
}];
}
</code></pre>
<p>当我尝试在我的 iOS 7 新应用程序中使用此代码并使用断点对其进行调试时,它从不执行方法 <code>- (void) addEventToCalendar:(EpisodeCell*)cell andCell:(int)cellID</code>。
这是为什么?我的代码有什么问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对于将事件添加到 native 日历,
包括这两个标题</p>
<pre><code> - #import <EventKit/EventKit.h>
- #import <EventKitUI/EventKitUI.h>
</code></pre>
<p>然后添加这些代码,</p>
<pre><code>EKEventStore *eventStore=[ init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
EKEvent *event= ;
NSString * NoteDetails =@"Event detail";
NSDate *startDate = ;
//Create the end date components
NSDateComponents *tomorrowDateComponents = [ init];
tomorrowDateComponents.day = 1;
NSDate *endDate = [ dateByAddingComponents:tomorrowDateComponents
toDate:startDate
options:0];
event.title =@"Your Event TITLE";
event.startDate=startDate;
event.endDate=endDate;
event.notes = appointmentDetail;
event.allDay=YES;
];
NSError *err;
;
}
else
{
NSLog(@"NoPermission to access the calendar");
}
}];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将事件添加到 iOS 7 日历,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21016926/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21016926/
</a>
</p>
页:
[1]