2013-01-15 6 views
1

내 앱의 만료일을 설정해야합니다. 에 대한 응용 프로그램이 열리고 코드 지금이내 앱의 만료 날짜를 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 
    } 
} 

처럼 갈 때 현재 은, 난 그냥 날짜를 계산하려면, 날짜를 저장하고 (내가 저장 한있는 내가 문자열처럼 저장하고있다) 현재 날짜가 1 개월이고 월이 지난 경우 업그레이드를하시기 바랍니다.

+0

스토어 날짜 즉 현재 날짜 + 30. 그리고 앱이 시작될 때마다 현재 날짜와 저장된 날짜를 비교합니다. 현재 날짜가 더 크면 팝업이 표시됩니다. – Iducool

+0

@ 어 맨 정말요? 너 정말 놀랍다. – trojanfoe

+0

한 가지 작업을 수행 할 수도 있습니다. 그것에 대한 로컬 알림을 추가하십시오. – Iducool

답변

2

날짜에 +1을 추가하여 다음 달을 찾기 위해 DateComponent를 사용하십시오.

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]; 

앱 출시가 userdefaults 당신의 PLIST를 읽고이 동일한 경우는 nextMonthDay를 확인할 때마다

는 다음이 만료되는 앱 쇼 경고를하고 응용 프로그램을 닫습니다/또는 모든 funcatinalities을 사용하지 않도록 설정합니다.

편집 : (. 즉 달의 일) (30)를 추가하여

//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 ***"); 
} 
+0

: 고마워요. 나는 그것을 지금 시도 할 것이다. –

+0

@all : 이제 잘 작동합니다. 감사합니다. Anoop –

+0

Anoop : NSDate * yourDate = edate.date; 내가 이런 식으로 사용하면 Incompatiple 유형 nsdate nsstring.but 같은 날짜 특성 only.but 코드를 사용하여 저장하는 경고가 잘 작동합니다. –

관련 문제