iOS/swift

11. [iOS/Swift] 앱 사용자에게 업데이트 알람 표시하기 - Siren 라이브러리

drizzle0925 2021. 6. 18. 17:49
728x90

개발환경

MacOS Big Sur 11.4

Xcode version 12.5

Swift version 5


개발자는 버그 수정을 위해 또는 새로운 기능 추가를 위해 앱을 업데이트하고 업데이트한 내용을 앱스토어에 다시 재 업로드합니다.

그러나 사용자는 앱스토어에 들어가기 전까지 업데이트 버전이 나왔는지 알 수 없습니다.

Siren 라이브러리를 활용해서 업데이트 버전 알림을 사용자에게 표시해보겠습니다.

 

Siren 설치

CocoaPods에 아래 코드를 추가 기입합니다.

pod 'Siren'

 

수정한 pod 내용을 적용합니다.

$ arch -x86_64 pod install

 

 

Swift Package Manager

.Package(url: "https://github.com/ArtSabintsev/Siren.git", majorVersion: 5)

 

빨간 상자로 체크한 부분을 지금부터 추가하겠습니다.

 

1) Xcode에서 프로젝트를 선택 > Targets에서 라이브러리를 추가할 타깃을 선택 > Frameworks, Libraries, and Embedded Content 쪽에 + 버튼을 클릭

 

2) Choose frameworks and libraries to add라는 팝업창이 생성 > 하단 드롭박스 메뉴에서 Swift Package Manager를 선택

 

3) Choose Package Repository라는 창이 생성 > github에서 SPM을 지원하는 라이브러리 주소를 입력 > 입력완료 후 Next 클릭

https://github.com/ArtSabintsev/Siren.git

 

4) 버전과 브랜치를 선택합니다. 전부 선택했다면 Next 클릭

 

5) 리소스를 가져왔으면 Finish를 클릭합니다. 

 

6) 다시 화면으로 돌아오면 패키지가 추가된 것을 확인할 수 있습니다.

 

그다음으로는 AppDelegate.swift에 필요한 코드를 작성하면 됩니다.

import Siren //라이브러리 추가
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
      window?.makeKeyAndVisible()
//    annoyingRule() // 다음에, 업데이트
      hyperCriticalRules() // 업데이트
        
        return true
    }
}

private extension AppDelegate {
    /// How to present an alert every time the app is foregrounded.
    func annoyingRule() {
        let siren = Siren.shared
        siren.apiManager = APIManager(country: .korea) //기준 위치 대한민국 앱스토어로 변경
        siren.presentationManager = PresentationManager(forceLanguageLocalization: .korean) //알림
        siren.rulesManager = RulesManager(globalRules: .annoying)

        siren.wail { results in
            switch results {
            case .success(let updateResults):
                print("AlertAction ", updateResults.alertAction)
                print("Localization ", updateResults.localization)
                print("Model ", updateResults.model)
                print("UpdateType ", updateResults.updateType)
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }

    /// How to present an alert every time the app is foregrounded.
    /// This will block the user from using the app until they update the app.
    /// Setting `showAlertAfterCurrentVersionHasBeenReleasedForDays` to `0` IS NOT RECOMMENDED
    /// as it will cause the user to go into an endless loop to the App Store if the JSON results
    /// update faster than the App Store CDN.
    ///
    /// The `0` value is illustrated in this app as an example on how to change how quickly an alert is presented.
    func hyperCriticalRules() {
        let siren = Siren.shared
        siren.apiManager = APIManager(country: .korea) //기준 위치 대한민국 앱스토어로 변경
        siren.presentationManager = PresentationManager(forceLanguageLocalization: .korean) //알림
        siren.rulesManager = RulesManager(globalRules: .critical,
                                          showAlertAfterCurrentVersionHasBeenReleasedForDays: 0)

        siren.wail { results in
            switch results {
            case .success(let updateResults):
                print("AlertAction ", updateResults.alertAction)
                print("Localization ", updateResults.localization)
                print("Model ", updateResults.model)
                print("UpdateType ", updateResults.updateType)
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}

 

메서드의 상세 내용은 아래 링크를 참조해주세요

https://github.com/ArtSabintsev/Siren/blob/master/Example/Example/AppDelegate.swift

 

알람 빈도, 옵션 변경

siren.rulesManager = RulesManager(majorUpdateRules: .critical,
                                  minorUpdateRules: .annoying,
                                  patchUpdateRules: .default,
                                  revisionUpdateRules: .relaxed)

RulesManager를 통해 알림 빈도와 생략 옵션 설정이 가능합니다.

각 옵션들은 아래와 같이 설정되어 있습니다.

 

annoying: 항상 확인, 다음에 업데이트 가능

critical: 항상 확인, 즉시 업데이트

default: 하루에 한 번 확인, 다음에 업데이트 가능, 이 버전 생략 가능

hinting: 일주일에 한 번 확인, 다음에 업데이트 가능

persistent: 하루에 한 번 확인, 다음에 업데이트 가능

relaxed: 일주일에 한 번 확인, 다음에 업데이트 가능, 이 버전 생략 가능

 

 

사용자가 직접 조건을 설정할 수도 있습니다.

siren.rulesManager = RulesManager(majorUpdateRules: Rules(promptFrequency: .immediately, forAlertType: .force),
                                  minorUpdateRules: Rules(promptFrequency: .daily, forAlertType: .option),
                                  patchUpdateRules: Rules(promptFrequency: .weekly, forAlertType: .skip),
                                  revisionUpdateRules: Rules(promptFrequency: .weekly, forAlertType: .none))
728x90