|
| 1 | +// |
| 2 | +// AppCenterSDK+SMFLogger.swift |
| 3 | +// Newsletter2Go |
| 4 | +// |
| 5 | +// Created by Konstantin Deichmann on 11.09.19. |
| 6 | +// Copyright © 2019 Newsletter2Go GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import AppCenter |
| 11 | +import AppCenterCrashes |
| 12 | +import AppCenterDistribute |
| 13 | + |
| 14 | +fileprivate enum AppCenterConstants { |
| 15 | + |
| 16 | + static let appSecretKey = "AppCenterAppSecret" |
| 17 | +} |
| 18 | + |
| 19 | +class AppCenterSDK: NSObject { |
| 20 | + |
| 21 | + // MARK: - Private static properties |
| 22 | + |
| 23 | + fileprivate static let isDebugBuild : Bool = { |
| 24 | + #if DEBUG |
| 25 | + return true |
| 26 | + #else |
| 27 | + return false |
| 28 | + #endif |
| 29 | + }() |
| 30 | + |
| 31 | + // MARK: - Private properties |
| 32 | + |
| 33 | + fileprivate var isInitialized = false |
| 34 | + fileprivate var configuration : Configuration? |
| 35 | + |
| 36 | + // MARK: - Public properties |
| 37 | + |
| 38 | + static var wasInitialized : Bool { |
| 39 | + return MSAppCenter.isConfigured() |
| 40 | + } |
| 41 | + |
| 42 | + // MARK: - Methods |
| 43 | + |
| 44 | + /// This will setup the SentrySDK with the common base configuration. Crashes will be detected if the app is build with the release build type and the sentry dsn taken from the info plists. |
| 45 | + /// |
| 46 | + /// - Parameters: |
| 47 | + /// - configuration: Configuration object |
| 48 | + static func setup(configuration: AppCenterSDK.Configuration = .default) { |
| 49 | + guard (self.isDebugBuild == false || configuration.enableDebug == true) else { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + let services = (configuration.isDistributionEnabled == true) ? [MSCrashes.self, MSDistribute.self] : [MSCrashes.self] |
| 54 | + |
| 55 | + MSAppCenter.start(configuration.appSecret, withServices: services) |
| 56 | + } |
| 57 | + |
| 58 | + /// This will create a `fatalError` to crash the app. |
| 59 | + static func performTestCrash() { |
| 60 | + |
| 61 | + MSCrashes.generateTestCrash() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +extension AppCenterSDK { |
| 66 | + |
| 67 | + struct Configuration { |
| 68 | + |
| 69 | + fileprivate var enableDebug : Bool = false |
| 70 | + fileprivate var appSecret : String = "" |
| 71 | + fileprivate var isDistributionEnabled : Bool |
| 72 | + |
| 73 | + /// Initializes a AppCenterSDK Configuration |
| 74 | + /// |
| 75 | + /// - Parameters: |
| 76 | + /// - appSecret: supply this manually if you dont want it in the info.plist |
| 77 | + init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil) { |
| 78 | + |
| 79 | + let appSecretFromBundle = Bundle.main.object(forInfoDictionaryKey: AppCenterConstants.appSecretKey) as? String |
| 80 | + |
| 81 | + guard let _appSecret = (appSecret ?? appSecretFromBundle) else { |
| 82 | + assertionFailure("Error: You have to set the `\(AppCenterConstants.appSecretKey)` key in the info plist or specify your own when initializing teh SDK.") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + self.enableDebug = enableDebug |
| 87 | + self.appSecret = _appSecret |
| 88 | + |
| 89 | + #if DEBUG |
| 90 | + self.isDistributionEnabled = isDistributionEnabled ?? false |
| 91 | + #elseif LIVE |
| 92 | + self.isDistributionEnabled = isDistributionEnabled ?? false |
| 93 | + #else |
| 94 | + self.isDistributionEnabled = isDistributionEnabled ?? true |
| 95 | + #endif |
| 96 | + } |
| 97 | + |
| 98 | + static var `default`: AppCenterSDK.Configuration { |
| 99 | + return Configuration() |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments