본문 바로가기

개발도구/iOS - 아이폰 개발

[ios] 저장 관련 NSNotificationCenter

ios에서 데이터를 저장하는 방법은 크게 세가지가 있는것 같다.

1. protocol / delegate 만들기 - http://blog.yagom.net/256

2. NSUserDefault 사용하기

3. NSNotificationCenter

notificationCenter는 내부 push라고 생각하면 좋다. 

푸시를 받아 처리하는 것은 http://blog.jidolstar.com/758 백그라운드 또는 앱이 off되어 있을경우 푸시를 받아 처리를 한다. 그런데 앱이 구동중일경우 푸시를 받게 될때 delegate에서 값을 받게 되고 tabbarcontroller 에 넘겨줄때 이미 처리된 탭에는 반응하질 못한다. 


푸시를 눌러 클릭할 경우 처음 탭에 접근할수가 있는데 그것은 NSUserDefault 을 처리하여 하였지만, 

앱이 실행되고 있는 경우는 탭의 변경이 어렵다. 그래서 반응 처리하기 위해 내부푸시 NSNotificationCenter로 작업하게 되었다. 내부푸시라고 불러도 될지 모르지만, 일단 앱내부안에서 반응하고 또한 앱과 앱끼리의 통신도 가능하니 참고하기 바란다. 


심플하게 NSNotificationCenter 사용은

데이터를 받는곳
// Add an observer that will respond to loginComplete
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(showMainMenu:) 
                                                 name:@"loginComplete" object:nil];

// the function specified in the same class where we defined the addObserver
- (void)showMainMenu:(NSNotification *)note {
    NSLog(@"Received Notification - Someone seems to have logged in"); 


데이터를 보내는 곳 // Post a notification to loginComplete [[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:nil];
이렇다. 

보다 자세한 설명은
http://whitegom.tistory.com/11
윗 사이트을 참고하기 바란다.

참고)

http://blog.yagom.net/267

http://www.youtube.com/watch?v=WB-QCv_4ANU