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) {
                }
            }
        });
    }];
}

效果图


提醒列表
提醒详情
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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