ios - 将我的应用程序的到期日期设置为 1 个月

标签 ios nsdate

必须为我的应用设置到期日期。 目前,我正在存储打开应用程序的日期,代码是这样的

NSError *error;
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"ExpiredDate" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entityDesc];
NSArray *fetchedObjects = [[context executeFetchRequest:fetchRequest error:&error] retain];

[fetchRequest release];

if([fetchedObjects count ]<=0)
{
    NSDate *todays=[[NSDate alloc]init];

    NSDateFormatter *date=[[NSDateFormatter alloc]init];
    [date setDateFormat:@"MM/DD/YYYY"];
    [date stringFromDate:todays];
   NSString *datelabel = [NSString stringWithFormat:@"%@",
                            [date stringFromDate:todays]];



    NSManagedObject *objUser;
    NSError *error;

    objUser = [NSEntityDescription insertNewObjectForEntityForName:@"ExpiredDate" inManagedObjectContext:context];
    [objUser setValue:datelabel forKey:@"date"];                 

    if (![context save:&error]) 
    {
        NSLog(@"Problem saving: %@", [error localizedDescription]);
    }



}
else
{
    for(int i=0; i< [fetchedObjects count]; i++)
    {
        edate = [fetchedObjects objectAtIndex:i];

    }

    NSLog(@"%@",edate.date); 
   if(onemonth)
    {
    alertview:if one month completed then i have to show a pop up.please upgrade            }
   else
    {
        my code should execute
    }
}

现在,我只想用当前日期计算一个月的日期(我已经存储了,我正在像字符串一样存储),如果过了一个月,那么我必须显示一个弹出窗口,比如请升级。

最佳答案

使用 DateComponent 通过将 +1 添加到您的日期来查找下个月。

NSDate *yourDate=...;
NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[[NSDateComponents alloc]init];
components.month=1;
NSDate *nextMonthDay=[calendar dateByAddingComponents:components toDate:yourDate options:0];

每次您的应用程序启动时,请阅读您的用户默认设置列表并检查下一个月日,如果它相等,则让您的应用程序显示它已过期的警报并关闭应用程序/或禁用所有功能。

编辑:

//I am setting it as today's date, assumed tha app installed today
NSDate *installedDate=[NSDate dateWithNaturalLanguageString:@"16/Dec/2012"]; //this method wont work on ios, convert using formatter.


//now finding and setting expiry date
NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[NSDateComponents new];
components.month=1;
NSDate *expiryDate=[calendar dateByAddingComponents:components toDate:installedDate options:0];

NSLog(@"Ins : %@, Exp : %@", installedDate, expiryDate);

if ([[NSDate date] isGreaterThanOrEqualTo:expiryDate]) {
    NSLog(@"*** Expired ***");
}
else{
    NSLog(@"*** This is trial version ***");
}

关于ios - 将我的应用程序的到期日期设置为 1 个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333482/

相关文章:

ios - 德尔福 + iOS : How to declare simple ObjC-Class?

ios - 使用 UIActivityViewController 提供不同格式的数据

ios - 如何在 Swift 中创建不漏边框的圆形头像或圆角图片?

ios - NSDate 格式没有显示预期的内容

ios - NSDateFormatter 在 iOS 7 上失败,适用于更高版本

ios - Swift - 有一个音频重复,但它在间隔播放?

ios - Swift TableViewCell xib 没有约束

ios - NSDateComponent 给我一天递增 1

ios - 如何按日期降序对 NSDate 进行排序,但在该日期内按时间升序排序?

ios - 弄错时差