반응형
Rx를 이용해 UIApplication 이벤트 감지
제가 생각한 방법은 NotificationCenter와 RxSwift를 결합하는 것 입니다.
아래는 예제입니다.
import UIKit
import RxSwift
import RxCocoa
class ViewController: UIViewController {
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// 포어그라운드로 전환될 때
NotificationCenter.default.rx.notification(UIApplication.willEnterForegroundNotification)
.subscribe(onNext: { _ in
print("앱이 포어그라운드로 전환되었습니다.")
// 필요한 작업 수행
})
.disposed(by: disposeBag)
// 백그라운드로 전환될 때
NotificationCenter.default.rx.notification(UIApplication.didEnterBackgroundNotification)
.subscribe(onNext: { _ in
print("앱이 백그라운드로 전환되었습니다.")
// 필요한 작업 수행
})
.disposed(by: disposeBag)
}
}
반응형
'iOS > RxSwift' 카테고리의 다른 글
[RxSwift] Rx를 이용한 페이징처리 (0) | 2024.06.25 |
---|---|
[RxSwift] Map, FlatMap (Combining Operators ) (0) | 2024.06.17 |
[RxSwift] ActivityIndicatorView를 Rx로 쉽게 컨트롤하기 (0) | 2024.06.08 |
[RxSwift] RxCocoa (binding, Traits, Driver..등) (0) | 2023.11.04 |
[RxSwift] Erro 처리 catch, retry 연산자 (0) | 2023.10.31 |