AcaMate_iOS/AcaMate/0. Setup/AppDelegate.swift

126 lines
4.3 KiB
Swift

//
// AppDelegate.swift
// AcaMate
//
// Created by Sean Kim on 11/26/24.
//
import SwiftUI
import UIKit
import UserNotifications
import KakaoSDKCommon
import KakaoSDKAuth
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
printLog("Start Set AppDelegate")
if let kakaoAppKey = KEY.loadKey(for: "kakaoAppKey") {
printLog("KAKAO APP KEY : \(kakaoAppKey)")
KakaoSDK.initSDK(appKey: kakaoAppKey)
}
//MARK: - Set Notification
let center = UNUserNotificationCenter.current()
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
center.requestAuthorization(options: authOptions) { granted, error in
if let error = error {
printLog("인증 오류: \(error.localizedDescription)")
}
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
printLog("알람 권한 해제.")
}
}
center.delegate = self
//MARK: -
_ = NetworkMonitor.shared
printLog("End Set AppDelegate")
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// if (AuthApi.isKakaoTalkLoginUrl(url)) {
// return AuthController.handleOpenUrl(url: url)
// }
//
return false
}
func registerForRemoteNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
print("Permission granted: \(granted)")
if let error = error {
printLog("권한 요청 실패 \(error)")
}
printLog("권한 : \(granted)")
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
//
func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02.2hX", $1)})
printLog("APNs 디바이스 토큰: \(deviceTokenString)")
//
}
//
func application(_ application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: Error) {
printLog("APNs 등록 실패: \(error)")
}
// ( )
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
let userInfo = notification.request.content.userInfo
if let apsData = userInfo["aps"] as? [AnyHashable: Any],
let badge = apsData["badge"] as? Int {
printLog("\(apsData)")
do {
try await UNUserNotificationCenter.current().setBadgeCount(badge)
} catch {
//
}
}
if #available(iOS 14.0, *) {
return [[.list,.banner,.sound]]
} else {
return[[.alert,.sound]]
}
}
// Notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let apsData = userInfo["aps"] as? [AnyHashable: Any] {
if let alert = apsData["alert"] as? [AnyHashable: Any] {
printLog("apsData: \(apsData)")
printLog("alert: \(alert)")
if let param = alert["parameter"] {
printLog(param as? String)
}
// viewModel.setBadge()
}
}
}
}