1

必须为我的应用程序设置到期日期。目前,我正在存储打开应用程序的日期,代码如下所示

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
    }
}

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

4

1 回答 1

2

使用 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 ***");
}
于 2013-01-15T08:33:49.873 回答