必须为我的应用设置到期日期。 目前,我正在存储应用程序打开的日期,代码就像这样
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
}
}
现在,我只想计算日期(我已经存储并且我像字符串一样存储)一个月的当前日期,如果月份越过那么我必须显示一个弹出窗口,请升级。
答案 0 :(得分: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];
每次你的应用程序启动时,请阅读你的用户默认值并检查nextMonthDay,如果它相等,则让你的应用程序显示它已过期,并关闭应用程序/或禁用所有功能。
编辑:
//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 ***");
}