iOS 앱을 개발할 때, 추적 접근 권한(ATTrackingManager)과 알림 권한(UNUserNotificationCenter)을 요청해야 하는 경우가 있습니다. 많은 개발자들이 이 작업을 AppDelegate의 applicationDidBecomeActive 메서드에서 수행하곤 합니다.
이 문제를 해결하기 위해서는 SceneDelegate의 sceneDidBecomeActive 메서드에서 권한 요청을 처리해야 합니다. 아래는 이를 구현한 코드입니다.
import UIKit
import AppTrackingTransparency
import UserNotifications
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func sceneDidBecomeActive(_ scene: UIScene) {
Task {
await requestTrackingAuthorization()
await requestNotificationAuthorization()
}
}
// 추적 접근 요청 메서드
func requestTrackingAuthorization() async {
let status = await ATTrackingManager.requestTrackingAuthorization()
switch status {
case .authorized: // 허용됨
print("Authorized")
case .denied: // 거부됨
print("Denied")
case .notDetermined: // 결정되지 않음
print("Not Determined")
case .restricted: // 제한됨
print("Restricted")
@unknown default: // 알려지지 않음
print("Unknown")
}
}
// 알림 권한 요청 메서드
func requestNotificationAuthorization() async {
let center = UNUserNotificationCenter.current()
do {
let granted = try await center.requestAuthorization(options: [.alert, .sound, .badge])
if granted {
print("알림 권한 허용")
} else {
print("알림 권한 거부")
}
} catch {
print("Error: \(error.localizedDescription)")
}
}
}
iOS 13 이후로 SceneDelegate를 사용하는 앱에서는 권한 요청을 AppDelegate 대신 SceneDelegate의 sceneDidBecomeActive 메서드에서 처리해야 합니다. 이를 통해 권한 요청이 제대로 이루어지도록 보장할 수 있습니다. 위의 코드를 참조하여 자신의 앱에 맞게 적용하면, 권한 요청과 관련된 문제를 쉽게 해결할 수 있습니다.