Skip to content

Commit

Permalink
#40 feat: AppDelegate에 알람 권한 여부에 대한 UserDefaults 값 설정하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sanghyeok-kim committed Jul 29, 2023
1 parent 3a18675 commit 851739a
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions Multimer/Multimer/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,37 @@ import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
UIApplication.shared.isIdleTimerDisabled = true

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (didAllow, error) in
switch didAllow {
case true:
UNUserNotificationCenter.current().delegate = self
case false:
//TODO: 알람 울리지 않음 경고 출력
break
}

let isFirstRun = !UserDefaults.standard.bool(forKey: Constant.UserDefaultsKey.isFirstRun)

switch isFirstRun {
case true:
UserDefaults.standard.set(true, forKey: Constant.UserDefaultsKey.isFirstRun)
NotificationCenter.default.post(name: .showSwipeToStopNotice, object: nil, userInfo: nil)
case false:
break
}
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound])
{ [weak self] (didAllow, error) in
UNUserNotificationCenter.current().delegate = self
self?.setNotificationAuthAlertPreference(didAllow: didAllow)
self?.postNotificationIfFirstLaunch()
}

return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
return UISceneConfiguration(
name: "Default Configuration",
sessionRole: connectingSceneSession.role
)
}

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
func application(
_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?
) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
}
Expand All @@ -58,3 +56,25 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
completionHandler([.banner, .sound])
}
}

// MARK: - Supporting Methods

private extension AppDelegate {
func setNotificationAuthAlertPreference(didAllow: Bool) {
let isFirstLaunch = !UserDefaults.standard.bool(forKey: Constant.UserDefaultsKey.isFirstLaunch)
let shouldShowNotificationAuthAlert = !isFirstLaunch && !didAllow
UserDefaults.standard.set(shouldShowNotificationAuthAlert, forKey: Constant.UserDefaultsKey.isNotificationAllowed)
}

func postNotificationIfFirstLaunch() {
let isFirstLaunch = !UserDefaults.standard.bool(forKey: Constant.UserDefaultsKey.isFirstLaunch)
if isFirstLaunch {
UserDefaults.standard.set(true, forKey: Constant.UserDefaultsKey.isFirstLaunch)
NotificationCenter.default.post(
name: .showSwipeToStopNotice,
object: nil,
userInfo: nil
)
}
}
}

0 comments on commit 851739a

Please sign in to comment.