zoukankan      html  css  js  c++  java
  • 添加事件提醒功能

    添加事件提醒功能

    效果

    源码

    //
    //  ViewController.m
    //  Event
    //
    //  Created by YouXianMing on 16/7/12.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CalendarEvent.h"
    
    @interface ViewController () <CalendarEventDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        
        NSTimeInterval hour        = 60.f * 60.f;
        NSDate        *currentDate = [NSDate date];
        
        CalendarEvent *event = [CalendarEvent new];
        event.eventTitle     = @"不作死就不会死";
        event.startDate      = [NSDate dateWithTimeInterval:hour * 1 sinceDate:currentDate];
        event.endDate        = [NSDate dateWithTimeInterval:hour * 2 sinceDate:currentDate];
        event.alarmDate      = [NSDate dateWithTimeInterval:hour * 0.5f sinceDate:currentDate];
        event.eventLocation  = @"大北京";
        event.delegate       = self;
        [event save];
    }
    
    - (void)calendarEvent:(CalendarEvent *)event removedStatus:(ECalendarEventStatus)status error:(NSError *)error {
    
        if (status == kCalendarEventAccessSavedSucess) {
            
            NSLog(@"保存成功");
        }
    }
    
    @end
    //
    //  CalendarEvent.h
    //  EventStore
    //
    //  Created by YouXianMing on 16/7/8.
    //  Copyright © 2016年 XianMing You. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class CalendarEvent;
    
    typedef enum : NSUInteger {
        
        /**
         *  Have not permission to save the event to system.
         */
        kCalendarEventAccessDenied = 1000,
        
        /**
         *  Saved the event success.
         */
        kCalendarEventAccessSavedSucess,
        
        /**
         *  Saved the event failed.
         */
        kCalendarEventAccessSavedFailed,
        
        /**
         *  Removed the event success.
         */
        kCalendarEventAccessRemovedSucess,
        
        /**
         *  Removed the event failed.
         */
        kCalendarEventAccessRemovedFailed,
        
    } ECalendarEventStatus;
    
    @protocol CalendarEventDelegate <NSObject>
    
    @optional
    
    /**
     *  The CalendarEvent saved status.
     *
     *  @param event  CalendarEvent's object.
     *  @param status Saved status.
     *  @param error  Error infomation.
     */
    - (void)calendarEvent:(CalendarEvent *)event savedStatus:(ECalendarEventStatus)status error:(NSError *)error;
    
    /**
     *  The CalendarEvent removed status.
     *
     *  @param event  CalendarEvent's object.
     *  @param status Removed status.
     *  @param error  Error infomation.
     */
    - (void)calendarEvent:(CalendarEvent *)event removedStatus:(ECalendarEventStatus)status error:(NSError *)error;
    
    @end
    
    @interface CalendarEvent : NSObject
    
    /**
     *  Event title.
     */
    @property (nonatomic, strong) NSString *eventTitle;
    
    /**
     *  Alarm date.
     */
    @property (nonatomic, strong) NSDate   *alarmDate;
    
    /**
     *  Event start date.
     */
    @property (nonatomic, strong) NSDate   *startDate;
    
    /**
     *  Event end date.
     */
    @property (nonatomic, strong) NSDate   *endDate;
    
    /**
     *  Event location.
     */
    @property (nonatomic, strong) NSString *eventLocation;
    
    /**
     *  CalendarEvent's delegate.
     */
    @property (nonatomic, weak)   id <CalendarEventDelegate>  delegate;
    
    /**
     *  Save the event to system.
     */
    - (void)save;
    
    /**
     *  Remove the event.
     */
    - (void)remove;
    
    /**
     *  To indicate the event have saved or not.
     *
     *  @return
     */
    - (BOOL)haveSaved;
    
    #pragma mark - Constructor method.
    
    + (instancetype)calendarEventWithEventTitle:(NSString *)title startDate:(NSDate *)startDate endDate:(NSDate *)endDate;
    
    @end
    //
    //  CalendarEvent.m
    //  EventStore
    //
    //  Created by YouXianMing on 16/7/8.
    //  Copyright © 2016年 XianMing You. All rights reserved.
    //
    
    #import "CalendarEvent.h"
    #import <CommonCrypto/CommonDigest.h>
    #import <EventKit/EventKit.h>
    
    @implementation CalendarEvent
    
    - (void)remove {
        
        NSString *eventId = [[NSUserDefaults standardUserDefaults] objectForKey:[self storedKey]];
        
        if (eventId) {
            
            EKEventStore *eventStore = [[EKEventStore alloc] init];
            EKEvent      *event      = [eventStore eventWithIdentifier:eventId];
            NSError      *error      = nil;
            [eventStore removeEvent:event span:EKSpanThisEvent error:&error];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:removedStatus:error:)]) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.delegate calendarEvent:self
                                   removedStatus:error ? kCalendarEventAccessRemovedFailed : kCalendarEventAccessRemovedSucess
                                           error:nil];
                });
            }
        }
    }
    
    - (BOOL)haveSaved {
        
        NSString *eventId = [[NSUserDefaults standardUserDefaults] objectForKey:[self storedKey]];
        
        if (eventId.length) {
            
            return YES;
            
        } else {
            
            return NO;
        }
    }
    
    - (void)save {
        
        NSParameterAssert(self.eventTitle);
        NSParameterAssert(self.startDate);
        NSParameterAssert(self.endDate);
        
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            
            if (granted) {
                
                EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
                event.calendar  = [eventStore defaultCalendarForNewEvents];
                event.title     = self.eventTitle;
                event.startDate = self.startDate;
                event.endDate   = self.endDate;
                
                self.alarmDate            ? [event addAlarm:[EKAlarm alarmWithAbsoluteDate:self.alarmDate]] : 0;
                self.eventLocation.length ? event.location = self.eventLocation                             : 0;
                
                NSError *savedError = nil;
                if ([eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&savedError]) {
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            [self.delegate calendarEvent:self
                                             savedStatus:kCalendarEventAccessSavedSucess
                                                   error:nil];
                        });
                    }
                    
                    // 存储事件的键值
                    [[NSUserDefaults standardUserDefaults] setObject:event.eventIdentifier forKey:[self storedKey]];
                    
                } else {
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            [self.delegate calendarEvent:self
                                             savedStatus:kCalendarEventAccessSavedFailed
                                                   error:savedError];
                        });
                    }
                }
                
            } else {
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        [self.delegate calendarEvent:self
                                         savedStatus:kCalendarEventAccessDenied
                                               error:error];
                    });
                }
            }
        }];
    }
    
    - (NSString *)storedKey {
        
        NSParameterAssert(self.eventTitle);
        NSParameterAssert(self.startDate);
        NSParameterAssert(self.endDate);
        
        NSString *string = [NSString stringWithFormat:@"%@%@%@", self.eventTitle, self.startDate, self.endDate];
        
        return [self md532BitLower:string];
    }
    
    - (NSString*)md532BitLower:(NSString *)string {
        
        const char    *cStr = [string UTF8String];
        unsigned char  result[16];
        CC_MD5(cStr, (unsigned int)strlen(cStr), result);
        
        return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                 result[0], result[1], result[2], result[3],
                 result[4], result[5], result[6], result[7],
                 result[8], result[9], result[10], result[11],
                 result[12], result[13], result[14], result[15]] lowercaseString];
    }
    
    + (instancetype)calendarEventWithEventTitle:(NSString *)title startDate:(NSDate *)startDate endDate:(NSDate *)endDate {
        
        CalendarEvent *event = [[self class] new];
        event.eventTitle     = title;
        event.startDate      = startDate;
        event.endDate        = endDate;
        
        return event;
    }
    
    @end
  • 相关阅读:
    提升工作效率的方法
    Spark Streaming 实现思路与模块概述
    Reduce Side Join实现
    File file:/data1/hadoop/yarn/local/usercache/hp/appcache/application_* does not exi
    Caused by: java.io.IOException: Filesystem closed的处理
    linux下nproc的作用
    Spark Streaming 的一些问题
    php代码审计7审计csrf漏洞
    php代码审计6审计xss漏洞
    php代码审计5审计命令执行漏洞
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5665041.html
Copyright © 2011-2022 走看看