创建提醒事项

 1 //
 2 //  ViewController.h
 3 //  Remind Me
 4 //
 5 //  Created by Hans-Eric Grönlund on 8/28/12.
 6 //  Copyright (c) 2012 Hans-Eric Grönlund. All rights reserved.
 7 //
 8
 9 #import <UIKit/UIKit.h>
10 #import <EventKit/EventKit.h>
11 #import <CoreLocation/CoreLocation.h>
12
13 typedef void(^RestrictedEventStoreActionHandler)();
14 typedef void(^RetrieveCurrentLocationHandler)(CLLocation *);
15
16 @interface ViewController : UIViewController<CLLocationManagerDelegate>
17 {
18     @private
19     CLLocationManager *_locationManager;
20     RetrieveCurrentLocationHandler _retrieveCurrentLocationBlock;
21     int _numberOfTries;
22 }
23
24 @property (weak, nonatomic) IBOutlet UIButton *addTimeBasedReminderButton;
25 @property (weak, nonatomic) IBOutlet UIButton *addLocationBasedReminderButton;
26 @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
27 @property (strong, nonatomic)EKEventStore *eventStore;
28
29 - (IBAction)addTimeBasedReminder:(id)sender;
30 - (IBAction)addLocationBasedReminder:(id)sender;
31
32 - (void)handleReminderAction:(RestrictedEventStoreActionHandler)block;
33 - (void)retrieveCurrentLocation:(RetrieveCurrentLocationHandler)block;
34
35 @end
  1 //
  2 //  ViewController.m
  3 //  Remind Me
  4 //
  5 //  Created by Hans-Eric Grönlund on 8/28/12.
  6 //  Copyright (c) 2012 Hans-Eric Grönlund. All rights reserved.
  7 //
  8
  9 #import "ViewController.h"
 10
 11 @interface ViewController ()
 12
 13 @end
 14
 15 @implementation ViewController
 16
 17 - (EKEventStore *)eventStore
 18 {
 19     if (_eventStore == nil)
 20     {
 21         _eventStore = [[EKEventStore alloc] init];
 22     }
 23     return _eventStore;
 24 }
 25
 26 - (void)viewDidLoad
 27 {
 28     [super viewDidLoad];
 29     // Do any additional setup after loading the view, typically from a nib.
 30 }
 31
 32 - (void)didReceiveMemoryWarning
 33 {
 34     [super didReceiveMemoryWarning];
 35     // Dispose of any resources that can be recreated.
 36 }
 37
 38 - (void)handleReminderAction:(RestrictedEventStoreActionHandler)block
 39 {
 40     self.addTimeBasedReminderButton.enabled = NO;
 41     self.addLocationBasedReminderButton.enabled = NO;
 42     [self.eventStore requestAccessToEntityType:EKEntityTypeReminder
 43                                     completion:^(BOOL granted, NSError *error)
 44      {
 45          if (granted)
 46          {
 47              block();
 48          }
 49          else
 50          {
 51              UIAlertView *notGrantedAlert = [[UIAlertView alloc] initWithTitle:@"Access Denied" message:@"Access to device‘s reminders has been denied for this app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 52
 53              dispatch_async(dispatch_get_main_queue(), ^{
 54                  [notGrantedAlert show];
 55              });
 56          }
 57          dispatch_async(dispatch_get_main_queue(), ^{
 58              self.addTimeBasedReminderButton.enabled = YES;
 59              self.addLocationBasedReminderButton.enabled = YES;
 60          });
 61      }];
 62 }
 63
 64 - (IBAction)addTimeBasedReminder:(id)sender
 65 {
 66     [self.activityIndicator startAnimating];
 67
 68     [self handleReminderAction:^()
 69     {
 70         // Create Reminder
 71         EKReminder *newReminder = [EKReminder reminderWithEventStore:self.eventStore];
 72         newReminder.title = @"Simpsons is on";
 73         newReminder.calendar = [self.eventStore defaultCalendarForNewReminders];
 74
 75         // Calculate the date exactly one day from now
 76         NSCalendar *calendar = [NSCalendar currentCalendar];
 77         NSDateComponents *oneDayComponents = [[NSDateComponents alloc] init];
 78         oneDayComponents.day = 1;
 79         NSDate *nextDay = [calendar dateByAddingComponents:oneDayComponents toDate:[NSDate date] options:0];
 80
 81         NSUInteger unitFlags = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
 82         NSDateComponents *tomorrowAt6PMComponents = [calendar components:unitFlags fromDate:nextDay];
 83         tomorrowAt6PMComponents.hour = 18;
 84         tomorrowAt6PMComponents.minute = 0;
 85         tomorrowAt6PMComponents.second = 0;
 86         NSDate *nextDayAt6PM = [calendar dateFromComponents:tomorrowAt6PMComponents];
 87
 88         // Create an Alarm
 89         EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:nextDayAt6PM];
 90         [newReminder addAlarm:alarm];
 91         newReminder.dueDateComponents = tomorrowAt6PMComponents;
 92
 93         // Save Reminder
 94         NSString *alertTitle;
 95         NSString *alertMessage;
 96         NSString *alertButtonTitle;
 97         NSError *error;
 98         [self.eventStore saveReminder:newReminder commit:YES error:&error];
 99         if (error == nil)
100         {
101             alertTitle = @"Information";
102             alertMessage = [NSString stringWithFormat:@"\"%@\" was added to Reminders", newReminder.title];
103             alertButtonTitle = @"OK";
104         }
105         else
106         {
107             alertTitle = @"Error";
108             alertMessage = [NSString stringWithFormat:@"Unable to save reminder: %@", error];
109             alertButtonTitle = @"Dismiss";
110         }
111
112         dispatch_async(dispatch_get_main_queue(), ^{
113             UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:alertButtonTitle otherButtonTitles:nil];
114             [alertView show];
115             [self.activityIndicator stopAnimating];
116         });
117
118     }];
119 }
120
121 - (void)retrieveCurrentLocation:(RetrieveCurrentLocationHandler)block
122 {
123     if ([CLLocationManager locationServicesEnabled] == NO)
124     {
125         UIAlertView *locationServicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"This feature requires location services. Enable it in the privacy settings on your device" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
126         [locationServicesDisabledAlert show];
127         return;
128     }
129
130     if (_locationManager == nil)
131     {
132         _locationManager = [[CLLocationManager alloc] init];
133         _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
134         _locationManager.distanceFilter = 1; // meter
135         _locationManager.activityType = CLActivityTypeOther;
136         _locationManager.delegate = self;
137     }
138     _numberOfTries = 0;
139     _retrieveCurrentLocationBlock = block;
140     [_locationManager startUpdatingLocation];
141 }
142
143 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
144 {
145     // Make sure this is a recent location event
146     CLLocation *lastLocation = [locations lastObject];
147     NSTimeInterval eventInterval = [lastLocation.timestamp timeIntervalSinceNow];
148     if(fabs(eventInterval) < 30.0)
149     {
150         // Make sure the event is accurate enough
151         if (lastLocation.horizontalAccuracy >= 0 && lastLocation.horizontalAccuracy < 20)
152         {
153             [_locationManager stopUpdatingLocation];
154             _retrieveCurrentLocationBlock(lastLocation);
155             return;
156         }
157     }
158     if (_numberOfTries++ == 10)
159     {
160         [_locationManager stopUpdatingLocation];
161         UIAlertView *unableToGetLocationAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Unable to get the current location." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
162         [unableToGetLocationAlert show];
163     }
164 }
165
166 - (IBAction)addLocationBasedReminder:(id)sender
167 {
168     [self.activityIndicator startAnimating];
169
170     [self retrieveCurrentLocation:
171      ^(CLLocation *currentLocation)
172      {
173          if (currentLocation != nil)
174          {
175              [self handleReminderAction:^()
176               {
177                   // Create Reminder
178                   EKReminder *newReminder = [EKReminder reminderWithEventStore:self.eventStore];
179                   newReminder.title = @"Buy milk!";
180                   newReminder.calendar = [self.eventStore defaultCalendarForNewReminders];
181
182                   // Create Location-based Alarm
183                   EKStructuredLocation *currentStructuredLocation = [EKStructuredLocation locationWithTitle:@"Current Location"];
184                   currentStructuredLocation.geoLocation = currentLocation;
185
186                   EKAlarm *alarm = [[EKAlarm alloc] init];
187                   alarm.structuredLocation = currentStructuredLocation;
188                   alarm.proximity = EKAlarmProximityLeave;
189
190                   [newReminder addAlarm:alarm];
191
192                   // Save Reminder
193                   NSString *alertTitle;
194                   NSString *alertMessage;
195                   NSString *alertButtonTitle;
196                   NSError *error;
197                   [self.eventStore saveReminder:newReminder commit:YES error:&error];
198                   if (error == nil)
199                   {
200                       alertTitle = @"Information";
201                       alertMessage = [NSString stringWithFormat:@"\"%@\" was added to Reminders", newReminder.title];
202                       alertButtonTitle = @"OK";
203                   }
204                   else
205                   {
206                       alertTitle = @"Error";
207                       alertMessage = [NSString stringWithFormat:@"Unable to save reminder: %@", error];
208                       alertButtonTitle = @"Dismiss";
209                   }
210
211                   dispatch_async(dispatch_get_main_queue(), ^{
212                       UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:alertButtonTitle otherButtonTitles:nil];
213                       [alertView show];
214                       [self.activityIndicator stopAnimating];
215                   });
216               }];
217          }
218      }];
219 }
220 @end

创建基于时间和基于位置的提醒事件,导入框架Event Kit与Core Location。

时间: 2024-08-24 17:38:44

创建提醒事项的相关文章

系统的日历和提醒事项的调用与交互(swift)

一.打开系统的app1.打开日历:if UIApplication.sharedApplication().canOpenURL(NSURL(string:"calshow:”)!) { UIApplication.sharedApplication().openURL(NSURL(string:"calshow:”)!) } 2.打开提醒事项:if UIApplication.sharedApplication().canOpenURL(NSURL(string:"x-ap

iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等(转)

转载自:http://www.cocoachina.com/ios/20151214/14502.html iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash. 当 用户在App启动时,看到弹出来的一条条“XXX 请求访问您的位置” “XXX 请求访问您的通讯录” “XXX 请求访问您的日历” “XXX 请求访问您的摄像头” 等一系列消息时,用户觉得不

Maven Web 项目创建注意事项

基本上只需要确认创建项目的Properties. (选中项目Alt+Enter) 1. 确认Java Build Path 中的Libraries 的JRE版本是否是你需要的版本,eclipse默认生成的为J2SE-1.5 2. 将Java Complier改为你当前的使用版本,如使用的jdk为1.7,则这里选则1.7 3. 如果使用web 3.0,则需要将webapp下的文件删除,打开项目Properties,点击Project Facets,将Java版本改为你当前使用版本(跟上一条保持一致

数据库表创建注意事项

一.      字段名及字段配制合理性 1.        剔除关系不密切的字段 2.        字段命名要有规则及相对应的含义(不要一部分英文,一部分拼音,还有类似a.b.c这样不明含义的字段) 3.        字段命名尽量不要使用缩写(大多数缩写都不能明确字段含义) 4.        字段不要大小写混用(想要具有可读性,多个英文单词可使用下划线形式连接) 5.        字段名不要使用保留字或者关键字 6.        保持字段名和类型的一致性 7.        慎重选择数

进程创建注意事项:函数及对应包

printf:#include<stdio.h> _exit(0):#include<unistd.h> file(fd):#include<fcntl.h> clone():#include<sched.h> malloc():#include<malloc.h> 最重要的:#define _GNU_SOURCE 参考: (1)未声明的CLONE_VM的解释: http://stackoverflow.com/questions/2244934

iOS 定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等授权检测

金田 iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash. 当用户在App启动时,看到弹出来的一条条“XXX 请求访问您的位置” “XXX 请求访问您的通讯录” “XXX 请求访问您的日历” “XXX 请求访问您的摄像头” 等一系列消息时,用户觉得不耐烦的同时,也会由于一时的安全考虑而把相应的功能给屏蔽掉,这还只是开始,当用户真正在使用对应功能的时候,

论文翻译之--- 软件设计师怎样使用标记来帮助提醒和重新查找

最近以来有个学习任务,就是翻译一篇关于软件工程相关的论文.我选择了一篇How Software Developers Tagging to Support Remingding and Refinding.由于本人水平有限,基本直译,有很多不准确不通之处.希望读到文章的人批评指导,大家交流改进,在此基础之上使得翻译和我个人的能力得以提高.我记录了自己工作的时间,确实这篇文章的翻译耗费了我一定的时间.文章来自IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, VO

我的Android 4 学习系列之创建用户基本界面

目录 使用视图和布局 理解Fragment 优化布局 创建分辨率无关的用户界面 扩展.分组.创建和使用视图 使用适配器将数据绑定到视图 使用视图和布局 1. Android UI 几个基本概念 视图: 所有可视界面的元素(通常称为控件或者小组件)的基类.所有的UI控件(包括布局类)都是由 View 派生而来的. 视图组:视图类的扩展,可以包含多个子视图.通过扩展ViewGroup类,可以创建由多个相互连接的子视图组成复合控件:还可以通过扩展ViewGroup类来提供布局管理器,以帮助在Aciti

100款开源软件

不知道这些你就OUT了!100款开源软件 好的软件可以极大提高工作效率,完成一些不可能完成工作.然而我们也常常为选择哪款软件而头疼,本次为大家推出的100款软件都是经过整理分类的,而且最重要的是100款软件都是开源免费且使用于Linux环境下,大部分都可以直接从官网上下载使用.如果你完全没有接触过以下我介绍的这100款软件,我只能说一句你OUT了! 图:100款软件分类图 接下来将要介绍的100款开源应用程序,是我花了很多时间为大家整理的,这些软件对大家来说是很有用的.我希望通过这100款软件告