【问题标题】:Application expiration using NSTimeInterval使用 NSTimeInterval 的应用程序过期
【发布时间】:2009-12-25 13:46:05
【问题描述】:

如何设置此方法以在应用程序首次启动 30 天后显示警报面板?

-(void)awakeFromNib

{

    NSDate * today = [NSDate date];

    NSTimeInterval expiry = ();


    if ([today timeIntervalSinceReferenceDate] > expiry){
        NSRunAlertPanel(@"Trial period has ended", @"Please Register", nil, nil, nil);
        NSLog(@"expired");
        [[NSApplication sharedApplication] terminate:self];
    }

}

【问题讨论】:

    标签: objective-c macos nstimeinterval


    【解决方案1】:

    使用NSDateComponents 上的day 属性来计算距当前日期一定数量的日期:

    NSDate *today = [NSDate date];
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = 30; // 30 days from launch
    
    NSDate *expDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:today options:0];
    

    仅应在应用首次打开时进行此项检查。 expDate 应该被持久化。每次应用在第一次之后重新启动时,都从第一次开始进行日期比较。

    // compare the dates
    if ([today compare:expDate] == NSOrderedDescending) {
        // trial expired
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2015-07-06
      • 2020-07-26
      • 1970-01-01
      • 2012-11-18
      • 2015-11-26
      • 1970-01-01
      相关资源
      最近更新 更多