zoukankan      html  css  js  c++  java
  • iOS工程中一天只让进行一次的操作如何做?

     转至: iosNSDateNSObject一天一次

    整体思路:当进行操作的时候记录操作时间存在偏好设置当中,当再次点击的时候获取现在的时间然后和之前记录的时间进行比较。如果是一天那么就提示“今天已经操作过了”,如果不是一天,那么可以正常操作,然后记录操作时间。如此循环往复。。。

    这里的一天只能操作一次指的是:

    (24点指的是凌晨12:00,也就是00:00)

    1、某一天任意时间点到24点之间。

    2、只要过了24点,立马可以进行第二次操作。

    3、假如你是23:59进行了一次操作,那么过了一分钟以后那就算第二天的了。所以可以进行第二次操作。

    4、所以记住:不是在24小时之内。而是某一天之内。

    首先我们要做的操作一般是网络请求,所以这里就拿网络请求为例,当我们点击按钮,触发方法inviteParent然后判断是否一天之内,如果不是,那么进行网络请求,那么此时今天的一次机会就用了。。。所以加入网络请求成功,那么此时记录现在的时间:

    主要方法:

    NSDate *nowDate = [NSDate date];
                   NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
                   [dataUser setObject:nowDate forKey:@"nowDate"];
                   [dataUser synchronize];

    代码示例:

    -(void)DoInviteParentsWithPersons:(NSArray *)array groups:(NSArray *)groupArray usersType:(NSInteger)userType
    {
       SendNoticeBody *notice = [[SendNoticeBody alloc]init];
       notice.msgType = 1;
       notice.msgSrcType = 0;    
       
       NSDictionary *destInfo = nil;
       destInfo = [[NSDictionary alloc]initWithObjectsAndKeys:notice.destPersons,@"person", nil];
       
       __weak __typeof(self)weakSelf = self;
       
       SendInvitePreNoticeSection *sec = [[SendInvitePreNoticeSection alloc]initWithMsgType:notice.msgType MsgSrcType:notice.msgSrcType DestInfo:destInfo NoticeContent:notice.msgContent SignName:notice.signName SourceId:notice.sourceId DepartFileIds:notice.fileIds ResultBlock:^(NSDictionary *dict, BOOL resultFlag, NSError *error) {
           if (resultFlag) {
               NSInteger resultCode = [dict JSONIntegerObjectForKey:@"resultCode"];
               if (resultCode == 1) {
                   [self showSuccessHubContent:@"邀请发送成功"];
                   NSDate *nowDate = [NSDate date];
                   NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
                   [dataUser setObject:nowDate forKey:@"nowDate"];
                   [dataUser synchronize];
               }else{
                   NSString *errmsg = [dict objectForKey:@"resultMsg"];
                   [self showErrorHubContent:errmsg];
               }
           }else{
           }
       }];
       [sec exec];
       
     
    }

    那么当第二次进行按钮点击的时候,就会获取现在的时间和之前记录的时间进行对比,如果是同一天,那么就不可以再次点击了。提示“每日仅能批量邀请一次”,按钮的点击时间代码如下:

    -(void)inviteParent
    {
    //一天之内只能批量邀请一次
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    // NSLog(@"之前时间:%@", [userDefault objectForKey:@"nowDate"]);//之前存储的时间
    // NSLog(@"现在时间%@",[NSDate date]);//现在的时间
    NSDate *now = [NSDate date];
    NSDate *agoDate = [userDefault objectForKey:@"nowDate"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *ageDateString = [dateFormatter stringFromDate:agoDate];
    NSString *nowDateString = [dateFormatter stringFromDate:now];
    // NSLog(@"日期比较:之前:%@ 现在:%@",ageDateString,nowDateString);
    if ( [ageDateString isEqualToString:nowDateString]) {
    [self showErrorHubContent:@"每日仅能批量邀请一次"];
    }else
    {
    [self DoInviteParentsWithPersons:array groups:groupArray usersType:nil];
    }
    }

    主要代码:

      

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    // NSLog(@"之前时间:%@", [userDefault objectForKey:@"nowDate"]);//之前存储的时间
    // NSLog(@"现在时间%@",[NSDate date]);//现在的时间
    NSDate *now = [NSDate date];
    NSDate *agoDate = [userDefault objectForKey:@"nowDate"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *ageDateString = [dateFormatter stringFromDate:agoDate];
    NSString *nowDateString = [dateFormatter stringFromDate:now];
    // NSLog(@"日期比较:之前:%@ 现在:%@",ageDateString,nowDateString);
    if ( [ageDateString isEqualToString:nowDateString]) {
    [self showErrorHubContent:@"每日仅能批量邀请一次"];
    }else
    {
    [self DoInviteParentsWithPersons:array groups:groupArray usersType:nil];
    }

     

  • 相关阅读:
    强化学习的基本迭代方法
    基于文本描述的事务聚类
    学习强化学习之前需要掌握的3种技能
    其它 华硕 ASAU S4100U 系统安装 win10安装 重装系统 Invalid Partition Table 解决
    数据分析 一些基本的知识
    Python 取样式的内容 合并多个文件的样式 自定义样式
    电商 Python 生成补单公司需要的评论格式3
    SpringBlade 本地图片上传 生成缩略图
    SQL Server 字符串截取
    SpringBlade 本地图片上传
  • 原文地址:https://www.cnblogs.com/henusyj-1314/p/6382718.html
Copyright © 2011-2022 走看看