如何利用 RxSwift 監聽進入背景/前景

RxSwift 是一個很好用的套用,當然也有監聽 App 進入背景和前景,舉例來說,如果我們要在程式進入前景時重新載入網頁,那麼用 RxSwift 可以輕鬆辦到。

swift 1

而以下是這個 Code 的寫法,可以寫在任何的地方監聽,如果有需要可以參考一下。

NotificationCenter.default
        .rx.notification(UIApplication.didEnterBackgroundNotification)
        .subscribe(onNext: { _ in
            //進入背景
        }).disposed(by: disposeBag) 
NotificationCenter.default
        .rx.notification(UIApplication.willEnterForegroundNotification)
        .subscribe(onNext: { _ in
            //進入前景
        }).disposed(by: disposeBag) 
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments