必须为我的应用设置到期日期。 目前,我正在存储打开应用程序的日期,代码是这样的
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/