Swift
내장된 notificationName 사용하여 notification 등록해보기
진태우
2018. 8. 21. 00:29
일반적으로 notification을 등록할 때 notificationName을 만들어 사용하는 경우가 많았다.
NSNotificationName에는 이미 구현되있어 신기하고 편하게 가져다 쓰면 유용한 것들이 있었다.
그 중에 스크린샷을 찍었을 때 사용되는 notificationName이 있어서 간단히 사용해 보았다.
class ViewController: UIViewController {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
registerForNotifications()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func registerForNotifications() {
NotificationCenter.default.addObserver(
self
, selector: #selector(performDidTask)
, name: NSNotification.Name.UIApplicationUserDidTakeScreenshot
, object: nil
)
}
func performDidTask() {
print("screenshot!!!!!")
}
}
viewController가 초기화 될 때 notification을 등록하는 코드를 작성하고 스크린샷을 찍는 순간 "screenshot!!!!"이 출력되는 것을 볼 수 있었다!!!
시간이 지날 때마다 호출되는 notificationName이 있을것 같아 찾아 보았지만.... 보이지 않았다.... 내 눈에만 안보이는건 아니겠지....
다른 여러가지 notificationName은 아래 애플 공식 홈페이지에서 찾아보고 적용해볼 수 있다!!!
Apple 공식 홈피 - NSNotoficationName