# [iOS] Быстрая настройка пушей

## Наследование от MindboxAppDelegate

Mindbox SDK предоставляет готовый AppDelegate, с готовыми настройками, чтобы надо было меньше настраивать. 

```swift {1}
final class AppDelegate: MindboxAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        super.application(application, didFinishLaunchingWithOptions: launchOptions)
        
        registerForRemoteNotifications()
        
        return true
    }
```

## Запрос авторизации

Создаем метод для запроса разрешения на пуши у пользователя, и передаем статус в Mindbox

```swift
    private func registerForRemoteNotifications() {
        UNUserNotificationCenter.current().delegate = self
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
            UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge]) { granted, error in
                print("Permission granted to allow local and remote notifications for your app: \(granted)")
                if let error = error {
                    print("NotificationsRequestAuthorization failed with error: \(error.localizedDescription)")
                }
                Mindbox.shared.refreshNotificationPermissionStatus()
            }
        }
    }
```

## Вызов метода

Добавьте метод `registerForRemoteNotifications` в `didFinishLaunchingWithOptions`. 

```swift /registerForRemoteNotifications/ /didFinishLaunchingWithOptions/
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        super.application(application, didFinishLaunchingWithOptions: launchOptions)
        
        registerForRemoteNotifications()
        
        return true
    }
```

## Отображение пуш уведомления

Для того чтоб пуш-уведомление отображалось, нужно выставить соответствующие настройки в методе 'willPresent'

```swift {3,5}
func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.list, .badge, .sound, .banner])
    }
}
```

## Пример готового кода

```swift
final class AppDelegate: MindboxAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        super.application(application, didFinishLaunchingWithOptions: launchOptions)
        
        registerForRemoteNotifications()
        
        return true
    }
    
    private func registerForRemoteNotifications() {
        UNUserNotificationCenter.current().delegate = self
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
            UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge]) { granted, error in
                print("Permission granted to allow local and remote notifications for your app: \(granted)")
                if let error = error {
                    print("NotificationsRequestAuthorization failed with error: \(error.localizedDescription)")
                }
                Mindbox.shared.refreshNotificationPermissionStatus()
            }
        }
    }
    
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.list, .badge, .sound, .banner])
    }
}

```