Skip to content

Commit

Permalink
[Add] #244 - notificationCenter & UIApplication extension 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongdung-eo committed Mar 21, 2024
1 parent ad50ab6 commit 24b0fbb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions iOS-NOTTODO/iOS-NOTTODO/Global/Extensions/NotificationCenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// NotificationCenter.swift
// iOS-NOTTODO
//
// Created by JEONGEUN KIM on 3/21/24.
//

import UIKit
import Combine

extension NotificationCenter {

enum Notification {
case willEnterForeground
case didEnterBackground
}

var willEnterForeground: AnyPublisher<NotificationCenter.Notification, Never> {
publisher(for: UIApplication.willEnterForegroundNotification)
.map { _ in return .willEnterForeground }
.eraseToAnyPublisher()
}

var didEnterBackground: AnyPublisher<NotificationCenter.Notification, Never> {
publisher(for: UIApplication.didEnterBackgroundNotification)
.map { _ in return .didEnterBackground }
.eraseToAnyPublisher()
}

var applicationState: AnyPublisher<NotificationCenter.Notification, Never> {
Publishers.Merge(willEnterForeground, didEnterBackground)
.eraseToAnyPublisher()
}
}
29 changes: 29 additions & 0 deletions iOS-NOTTODO/iOS-NOTTODO/Global/Extensions/UIApplication+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UIApplication+.swift
// iOS-NOTTODO
//
// Created by JEONGEUN KIM on 3/21/24.
//

import UIKit

extension UIApplication {
private static let notificationSettingsURL: URL? = {
let settingsString: String
if #available(iOS 16, *) {
settingsString = UIApplication.openNotificationSettingsURLString
} else if #available(iOS 15.4, *) {
settingsString = UIApplicationOpenNotificationSettingsURLString
} else {
settingsString = UIApplication.openSettingsURLString
}
return URL(string: settingsString)
}()

func openAppNotificationSettings() {
guard
let url = UIApplication.notificationSettingsURL,
self.canOpenURL(url) else { return }
return open(url)
}
}

0 comments on commit 24b0fbb

Please sign in to comment.