clamp
Clamp
clamp
글쓰기 관리
전체 방문자
오늘
어제
  • 분류 전체보기 (509)
    • IOS (85)
    • SwiftUI+TCA+Combine (9)
    • RxSwift + MVVM (56)
    • Clean Architecture (12)
    • SWIFT (56)
    • iOS - TDD (2)
    • 디자인패턴 (4)
    • CS (56)
      • 알고리즘 (29)
      • 운영체제 (15)
      • 자료구조 (2)
      • 네트워킹 (4)
      • 기타 (6)
    • 회고 (0)
    • Firebase (18)
    • SwiftUI (10)
    • iOS - UIKit (11)
    • iOS - 오픈소스 (6)
    • 코딩테스트 (166)
      • 프로그래머스 (164)
    • 정보처리기사 (14)
    • GitHub (2)
글쓰기 / 관리자

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Q
  • ㅅ
  • uikit
  • Swift

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
clamp

Clamp

IOS

IOS - 사용자에게 알람 승인을 구하는 법.

2022. 4. 24. 13:30

우리들은 앱을 처음 설치하고 열었을 때 "이 앱에서 오는 알림을 받으시겠습니까?" 하는 별도에 Alert을 기억 할텐데 그런 식으로 만약에 사용자가 허용을 하지 않으면 우리가 잘 설정해 놓은 이런 알람도 사실상 앱에 표현되지 않는다.

어쨋든 이러한 허용을 받아야만 보낼 수 있는 구조로 되어있기 때문에, 사용자의 승인을 구하는 코드를 추가해야한다.

 

AppDelegate.swift파일에서 UserNotifications를 추가해준다.

//AppDelegate.swift
import UIKit
import UserNotifications

 

그리고 didFinishLaunchingWithOption에서 구현한다.

class AppDelegate: UIResponder, UIApplicationDelegate {
	
    //UNUserNotificationCenter객체를 생성.
    var userNotificationCenter: UNUserNotificationCenter?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        UNUserNotificationCenter.current().delegate = self
        
        //notification Option들을 생성 하는데 arrayLiteral로 해서, alert, badge, sound에 대한 허락을 구한다.
        let authrizationOptions = UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])
        
        //userNotificationCenter는 인증을 request(요청)할 수 있다. 옵션은 위에 생성해놓은 옵션이고, 
        //completionHandler를 통해서 에러처리를 할 수 있다. 결과값은 무시하고 에러 처리정도만 해줌.
        userNotificationCenter?.requestAuthorization(options: authrizationOptions){ _, error in
            if let error = error{
                print("ERROR: notification authrization request \(error.localizedDescription)")
            }
        }
        return true
    }
저작자표시 비영리 동일조건 (새창열림)
    'IOS' 카테고리의 다른 글
    • IOS - Navigation Bar Title 변경하는 방법
    • IOS - PickerView
    • IOS - 화면 터치 시 키보드 내려가기
    • IOS - AutoLayout의 Hugging, Compression Resistance Priority
    clamp
    clamp
    주니어 iOS개발자의 발악!!!!!!!

    티스토리툴바