투케이2K

35. (Objective-C/objc) NSNotificationCenter 노티피케이션 브로드 캐스팅 채널 알림 등록, 해제, 메시지 전송 실시 본문

Objective-C

35. (Objective-C/objc) NSNotificationCenter 노티피케이션 브로드 캐스팅 채널 알림 등록, 해제, 메시지 전송 실시

투케이2K 2022. 6. 23. 18:02

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController





// MARK: - [뷰 로드 실시]
- (void)viewDidLoad {
    [super viewDidLoad];
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> viewDidLoad() :: 뷰 로드 실시] \n");
    printf("==================================== \n");
    printf("\n");
    
    // MARK: [노티피케이션 알림 채널 등록]
    NSNotificationCenter *simpleAuthNotification = [NSNotificationCenter defaultCenter]; // [객체 선언 실시]
    [simpleAuthNotification addObserver:self selector:@selector(simpleAuthNotification:) name:@"simpleAuthNotification" object:nil]; // [채널 등록 실시]
}





// MARK: - [뷰 로드 완료]
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> viewWillAppear() :: 뷰 로드 완료] \n");
    printf("==================================== \n");
    printf("\n");
    
    // MARK: [노티피케이션 알림 전송 실시]
    NSNotificationCenter *simpleAuthNotification = [NSNotificationCenter defaultCenter]; // [객체 선언 실시]
    
    NSDictionary *sendDic = [NSDictionary dictionaryWithObject:@"TWOK 입니다" forKey:@"message"]; // [딕셔너리 데이터]
    
    [simpleAuthNotification postNotificationName:@"simpleAuthNotification" object:self userInfo:sendDic]; // [알림 전송]
}





// MARK: - [뷰 화면 표시]
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> viewDidAppear() :: 뷰 화면 표시] \n");
    printf("==================================== \n");
    printf("\n");
}





// MARK: - [뷰 정지 상태]
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> viewWillDisappear() :: 뷰 정지 상태] \n");
    printf("==================================== \n");
    printf("\n");
    
    // MARK: [노티피케이션 알림 채널 해제]
    NSNotificationCenter *simpleAuthNotification = [NSNotificationCenter defaultCenter]; // [객체 선언 실시]
    [simpleAuthNotification removeObserver:self name:@"simpleAuthNotification" object:nil]; // [채널 해제 실시]
}





// MARK: - [뷰 종료 상태]
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> viewDidDisappear() :: 뷰 종료 상태] \n");
    printf("==================================== \n");
    printf("\n");
}




// MARK: - [실시간 노티피케이션 알림 수신 (받는) 부분]
-(void)simpleAuthNotification:(NSNotification *)notification {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> simpleAuthNotification() :: 채널 알림 전달 받음] \n");
    printf("==================================== \n");
    printf("\n");
    
    // [딕셔너리로 전달 받은 데이터 파싱]
    NSString *msgData = [[notification userInfo] objectForKey:@"message"];
    
    printf("\n");
    printf("==================================== \n");
    printf("[msgData : %s] \n", msgData.description.UTF8String);
    printf("==================================== \n");
    printf("\n");
}


@end
 

[결과 출력]


 
반응형
Comments