2014-12-16 3 views
0

내 앱을 백그라운드 모드로 설정하면 값을 NSUserDefaults에 저장 한 다음 앱이 다시 활성화되면 값이 달라집니다.배경으로 돌아 가면 NSUserDefaults가 달라집니다.

- (void)appDidEnterBackground:(NSNotification *)notification { 
    //Tiempo inicial de inactividad 

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults]; 
    NSTimeInterval timestamp = ([[NSDate date] timeIntervalSince1970] * 1000); 

    [dispositivo setFloat:timestamp forKey:@"StartBackground"]; 

    NSLog(@"Start background: %f", timestamp); 

    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

로그 :

나는 값을 저장

- (void)appDidBecomeActive:(NSNotification *)notification { 

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults]; 
    NSTimeInterval startDate = [dispositivo floatForKey:@"StartBackground"]; 
    NSLog(@"Start date: %f", startDate); 
} 

로그 : 시작 날짜 : 내가 값을 복구 할 1418731653366.276123

: 배경 시작 1418731716608.000000

이 값은이 곳에서만 사용합니다. 진심으로 감사드립니다. 당신이 [있는 NSDate 날짜] ... 그것은 현재 시간을 업데이트됩니다 ... 사용하고 있기 때문에

+1

을 저장하는 것이 바람직. – ZeMoon

+0

double 값을 float 대신 저장해야합니다! –

+0

결과가 동일합니다. – amurcia

답변

0

거기에 아무것도 잘못되었는지

+0

[NSDate date]를 사용하여 나중에 백그라운드로 입력하기 전에 날짜를 저장합니다. 나중에 nsuserdefaluts의 값을 읽으려고합니다 – amurcia

+0

http://stackoverflow.com/questions/2013850/whats-the-optimum-way-of-storing-an-nsdate-in-nsuserdefaults...go 이것을 통해 한 번 – madhu

2

NSTimeInterval가 두 번 눌러에 당신이

typedef double NSTimeInterval; 
을 얻을 것이다

은 그래서 당신은하지 부동

[dispositivo setDouble:timestamp forKey:@"StartBackground"]; 

이중에서 값을 저장해야합니다 및 데이터

,925를 얻을 수

의 당신은 단순히 플로트로 저장하는 대신 NSUserDefaults에있는 NSDate 객체 자체를 저장할 수 NSDate 객체 :

+0

거기 있습니다. NSUserDefaults와 연결된 setDouble 메서드가 없습니다. – ZeMoon

+1

@ZeMoon 확인하십시오. –

+0

네 말이 맞아 ... 내 잘못 : P – ZeMoon

관련 문제