[iOS] NSNotificationCenter가 앱이 백그라운드에서 포그라운드로 이동했는지 확인 방법.
본문
iOS NSNotificationCenter가 앱이 백그라운드에서 포그라운드로 이동했는지 확인 방법.
[objective-c]
알림 신청
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourUpdateMethodGoesHere:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
호출해야하는 코드를 구현.
- (void) yourUpdateMethodGoesHere:(NSNotification *) note {
// code
}
뷰컨트롤러 종료시 사용한 알림은 삭제 한다.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[swift3]
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self,
selector:#selector(applicationWillEnterForeground(_:)),
name:NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
func applicationWillEnterForeground(_ notification: NSNotification) {
....
}
[swift4.2]
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification
, object: nil)
[swift4.1]
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(enterForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
@objc func enterForeground() {
// Do stuff
}
댓글목록 0