iOS 根据excel表格自动添加系统提醒事项

前言

姐姐需要把公司的一些证件审核时间录入到手机的提醒事项,避免时间过了忘了,于是问我能不能弄一个出来,于是便开始捣鼓这个东西。

准备工作

首先比较复杂的是excel表格的解析,因为苹果官方不提供excel表格的解析,网上也找不到相应的框架,于是便找了一些资料,进行excel表格的解析和录入。

样表

序号 姓名 性别 证号 工种 领证日期 第一次复核时间 第二次复核时间 使用截止时间
1 阮华珍 桂Dxxxxxxxxx 建筑起重机械司机(施工升降机) 2011年7月3日 2013年7月20日 2015年7月20日 2017年7月20日

正式开始

  1. excel的读取
    首先,excel的读取我采取的是苹果提供的 使用其他应用程序打开的功能,因为苹果不像安卓可以数据共享,因此我采取的就是通过qq,微信或者百度云之类的把excel表格下载下来,然后使用我的app打开进行数据的传输。
  • info.plist添加以下代码,让我们的app可以使用打开excel表格的功能
<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeIconFiles</key>
            <array>
                <string>MySmallIcon.png</string>
                <string>MyLargeIcon.png</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>My File Format</string>
            <key>LSHandlerRank</key>
            <string>Owner</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.microsoft.powerpoint.ppt</string>
                <string>public.item</string>
                <string>com.microsoft.word.doc</string>
                <string>com.adobe.pdf</string>
                <string>com.microsoft.excel.xls</string>
                <string>public.image</string>
                <string>public.content</string>
                <string>public.composite-content</string>
                <string>public.archive</string>
                <string>public.audio</string>
                <string>public.movie</string>
                <string>public.text</string>
                <string>public.data</string>
            </array>
        </dict>
    </array>
  • 然后当我们打开一个excel的时候,会在appDelegate会自动调用代理回调,这里我采用的是通知进行excel路径的传递
    - (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
   if (options) {
     NSString *str = [NSString stringWithFormat:@"\n发送请求的应用程序的 Bundle ID:%@\n\n文件的NSURL:%@", options[UIApplicationOpenURLOptionsSourceApplicationKey], url];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"url":[url absoluteString]}];
       }
       return YES;
 }
  • 然后在相应的地方进行接收通知就可以拿到相应的excel地址
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(analycsExcel:) name:@"test" object:nil];
  1. excel的解析
    拿到excel的路径,我们就可以进行excel的解析了,但是现在网上关于这块的资料是很少的,然后我找了好久,找到一个封装的比较好的demo进行excel的解析。
    碎月Coder的文章
    这个demo的思路就是利用DHxlsReader(解读XLS)进行xls的解析,因为只是个人的app,所以对于文件格式要求不严格,所以就没有处理xlsx的格式,只是处理了xls的格式。
  • 核心代码
    -  (void)analycsExcel:(NSNotification *)noc
   {
   
   NSString *path = [noc.userInfo[@"url"] substringFromIndex:15];
   
   DHxlsReader *reader = [DHxlsReader xlsReaderFromFile:path] ;
   assert(reader);

   int row = 2;
   while(YES) {
       
       CellModel *model = [CellModel new];
       
       DHcell *blankCell = [reader cellInWorkSheetIndex:0 row:row col:2];
       
       if (blankCell.type == cellBlank)
       {
           break;
       }
       
       for (int i = 2; i < 10; ++i) {
           DHcell *cell = [reader cellInWorkSheetIndex:0 row:row col:i];
           
           switch (i) {
               case 2:
                   model.name = cell.str;
                   break;
               case 3:
                   model.sex = cell.str;
                   break;
               case 4:
                   model.number = cell.str;
                   break;
               case 5:
                   model.tpye = cell.str;
                   break;
               case 6:
                   [model changeWithGotTime:cell.str];
                   break;
               case 7:
                   [model changeWithfirstTime:cell.str];
                   break;
               case 8:
                   [model changeWithsecTime:cell.str];
                   break;
               case 9:
                   [model changeWithfinalTime:cell.str];
                   break;
               default:
                   break;
           }
       }
       
       NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
       [tempFormatter setDateFormat:@"yyyy-MM-dd"];
       
       NSString *date =  [tempFormatter stringFromDate:model.gotTime];
       NSString *finalDate = [tempFormatter stringFromDate:model.finalTime];
       
       NSString *firstTitle = [NSString stringWithFormat:@"%@ 第一次复核",model.name];
       NSString *firstNotes = [NSString stringWithFormat:@"第一次复核\n%@、%@\n%@\n%@\n领证日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
       [[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:firstTitle notes:firstNotes];
       
       NSString *secTitle = [NSString stringWithFormat:@"%@ 第二次复核",model.name];
       NSString *secNotes = [NSString stringWithFormat:@"第二次复核\n%@、%@\n%@\n%@\n领证日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
       [[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:secTitle notes:secNotes];
       
       NSString *finalTitle = [NSString stringWithFormat:@"%@ 使用截止日期",model.name];
       NSString *finalNotes = [NSString stringWithFormat:@"使用截止日期\n%@、%@\n%@\n%@\n领证日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
       [[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:finalTitle notes:finalNotes];
       
       [self.cellArray addObject:model];
       row++;
   }
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
   [alert show];
}
  • 单例工具类里面添加提醒事项的方法
   - (void)addReminderNotify:(NSDate *)date title:(NSString *)title notes:(NSString *)notes
{
            __weak __typeof__(self) weakSelf = self;
    
            //申请提醒权限
    
            [self.eventDB requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {

            dispatch_async(dispatch_get_main_queue(), ^{
            __strong __typeof__(weakSelf) strongSelf = weakSelf;
            
            if (granted) {
                //创建一个提醒功能
                
                EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventDB];
                //标题
                
                reminder.title = title;
                reminder.notes = notes;
                //添加日历
                
                [reminder setCalendar:[self.eventDB defaultCalendarForNewReminders]];
                
                NSCalendar *cal = [NSCalendar currentCalendar];
                
                [cal setTimeZone:[NSTimeZone systemTimeZone]];
                
                NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth |
                
                NSCalendarUnitDay;
                
                NSDateComponents* dateComp = [cal components:flags fromDate:date];
                NSDateComponents* dateComp2 = [cal components:flags fromDate:[NSDate dateWithTimeInterval:88000 sinceDate:date]];
                
                dateComp.timeZone = [NSTimeZone systemTimeZone];
                
                reminder.startDateComponents = dateComp; //开始时间
                
                reminder.dueDateComponents = dateComp2; //到期时间
                
                reminder.priority = 1; //优先级
                
                EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; //添加一个车闹钟
                
                [reminder addAlarm:alarm];
                
                NSError *err;
                
                [strongSelf.eventDB saveReminder:reminder commit:YES error:&err];
                
                if (err==nil) {
                }
            }
        });
    }];
}

效果图


提醒列表
提醒详情
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,107评论 29 470
  • /**ios常见的几种加密方法: 普通的加密方法是讲密码进行加密后保存到用户偏好设置( [NSUserDefaul...
    彬至睢阳阅读 2,883评论 0 7
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,109评论 1 6
  • 李一十八阅读 242评论 0 0
  • 10多年前,人们都担心80后无法从60后70后那接过责任的重担,但是08年抗震救灾时,以80后为主的年青一代义...
    那年的夏阅读 4,057评论 5 15