2

如何设置此方法以在应用程序首次启动 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];
    }

}
4

1 回答 1

0

使用dayon 属性NSDateComponents计算一个日期,从当前日期算起一定数量的日期:

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
}
于 2015-12-18T21:00:42.200 回答