美文网首页
APP每天第一次启动

APP每天第一次启动

作者: ShawMoolan | 来源:发表于2019-11-18 14:35 被阅读0次
    /**
     *   每天第一次启动app去签到
     */
    -(BOOL)goToSign{
     
        NSDate *now = [NSDate date];
        //当前时间的时间戳
        NSTimeInterval nowStamp = [now timeIntervalSince1970];
        //当天零点的时间戳
        NSTimeInterval zeroStamp = [[[NSUserDefaults standardUserDefaults] objectForKey:@"zeroStamp"] doubleValue];
        //一天的时间戳
        NSTimeInterval oneDay = 60* 60 * 24;
        
        /**
            "hasSign"代表了是否当天是否签过到,NO代表没有签过,YES代表已经签过
         */
    
        if(nowStamp - zeroStamp > oneDay){
            
            zeroStamp = [self getTodayZeroStampWithDate:now];
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:zeroStamp] forKey:@"zeroStamp"];
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"hasSign"];
            return YES;
            
        }else{
            
            if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hasSign"]) {
                
                return NO;
                
            } else {
                
                return YES;
                
            }
        }
    }
    
    /**
     * 获取当天零点时间戳
     */
    - (double)getTodayZeroStampWithDate:(NSDate *)date{
     
        NSDateFormatter *dateFomater = [[NSDateFormatter alloc]init];
        dateFomater.dateFormat = @"yyyy年MM月dd日";
        NSString *original = [dateFomater stringFromDate:date];
        NSDate *ZeroDate = [dateFomater dateFromString:original];
        // 今天零点的时间戳
        NSTimeInterval zeroStamp = [ZeroDate timeIntervalSince1970];
        return zeroStamp;
    
    }
    

    相关文章

      网友评论

          本文标题:APP每天第一次启动

          本文链接:https://www.haomeiwen.com/subject/daxbictx.html