添加事件提醒功能

来源:http://www.cnblogs.com/YouXianMing/archive/2016/07/12/5665041.html
-Advertisement-
Play Games

添加事件提醒功能 效果 源碼 ...


添加事件提醒功能

 

效果

 

源碼

//
//  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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 讓你的 EditText 全部清除: 網址:https://github.com/MrFuFuFu/ClearEditText ...
  • 原文:ListView滑動刪除 ,仿騰訊QQ(鴻洋_) 文章實現的功能是:在ListView的Item上從右向左滑時,出現刪除按鈕,點擊刪除按鈕把Item刪除。 看過文章後,感覺沒有必要把dispatchTouchEvent()和onTouchEvent()兩個方法都重寫,只要重寫onTouchEv ...
  • 偶爾逛簡書能看見很多值得記下來的東西,有的接觸過有的沒接觸過,接觸過的也可能過段時間就忘記了,再上手的時候可能手足無措,所以決定有些覺得值得記下來的東西還是記錄一下。博客是個好地方,因為很多人都能搜索到,所以迫使自己要認真點寫,不然就很丟人了,所以在這裡記錄一些東西可能是對自己而言以後再次翻看最清晰 ...
  • 整理了一些應用加固評測。總共選擇了5個平臺,1款APP。同時用這5個平臺加固,然後通過操作體驗,加固後啟動時間,加固後應用大小和相容性上進行評測比較。 ...
  • 一,工程圖。 二,代碼。 RootViewController.m -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* 1,不使用動畫 UIViewAnimationTransitionNone 2,從左向右旋轉翻 ...
  • 1.數組排序有很多方法比如for,while迴圈去進行冒泡排序或者快速看、排序等多種排序方法 而我在這裡要說的是蘋果API提供的幾個系統方法 a.迭代器 Descriptor b.方法比較 Selector c.函數比較 Function d.塊代碼 Block自定義 2.示例 1.1一個要比較對象 ...
  • instanceof是Java的一個二元操作符,和==,>,<是同一類東東。由於它是由字母組成的,所以也是Java的保留關鍵字。它的作用是測試它左邊的對象是否是它右邊的類的實例,返回boolean類型的數據。舉個例子: String s = "I AM an Object!"; boolean is ...
  • 在這裡跟大家分享一個有趣的項目。 這個項目提供一個實時地球照片源,通過向其伺服器發送請求,能抓取到當前地球的照片。對於圖片壁紙類的應用來說是一個不錯的圖片源選擇。 該項目獲取照片也提供了很多線上操作的功能,比如說,可以獲取不同解析度的照片,最大解析度可達到 2200*2200。能線上進行裁剪、壓縮、 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...