|
| 1 | +// |
| 2 | +// AppCenterSDK+SMFLogger.swift |
| 3 | +// SmartMobileFactory |
| 4 | +// |
| 5 | +// Created by Konstantin Deichmann on 11.09.19. |
| 6 | +// Copyright © 2019 SmartMobileFactory. 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 AppCenterSDK with the common base configuration. Crashes will be detected if the app is build with the release build type. |
| 45 | + /// Distribution can be enabled using the configuration |
| 46 | + /// |
| 47 | + /// - Parameters: |
| 48 | + /// - configuration: Configuration object |
| 49 | + static func setup(configuration: AppCenterSDK.Configuration = .default) { |
| 50 | + guard (self.isDebugBuild == false || configuration.enableDebug == true) else { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + let services = (configuration.isDistributionEnabled == true) ? [MSCrashes.self, MSDistribute.self] : [MSCrashes.self] |
| 55 | + |
| 56 | + MSAppCenter.start(configuration.appSecret, withServices: services) |
| 57 | + } |
| 58 | + |
| 59 | + /// Returns True, if and only if the Service got started and is enabled. |
| 60 | + var isDistributionEnabled: Bool { |
| 61 | + return MSDistribute.isEnabled() |
| 62 | + } |
| 63 | + |
| 64 | + /// Will enable or disable the Distribution Feature of AppCenter |
| 65 | + /// While disabling works always. |
| 66 | + /// For enabling the Setup (aka. start) - Method should be called before. |
| 67 | + /// |
| 68 | + /// Flow in the App, for Apps that want to dynamically change that State: |
| 69 | + /// - Call the Setup Method for with `isDistributionEnabled` set to true. |
| 70 | + /// - Disable Distribution using `enableDistribution(enabled: false)` |
| 71 | + /// - Enable Distribution at a later time using the same method |
| 72 | + /// |
| 73 | + /// - Parameter enabled: Enable or Disable Distribtion |
| 74 | + func enableDistribution(enabled: Bool = true) { |
| 75 | + MSDistribute.setEnabled(enabled) |
| 76 | + } |
| 77 | + |
| 78 | + /// This will create a `fatalError` to crash the app. |
| 79 | + static func performTestCrash() { |
| 80 | + |
| 81 | + MSCrashes.generateTestCrash() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +extension AppCenterSDK { |
| 86 | + |
| 87 | + struct Configuration { |
| 88 | + |
| 89 | + fileprivate var enableDebug : Bool |
| 90 | + fileprivate var appSecret : String |
| 91 | + fileprivate var isDistributionEnabled : Bool |
| 92 | + |
| 93 | + /// Initializes a AppCenterSDK Configuration |
| 94 | + /// |
| 95 | + /// - Parameters: |
| 96 | + /// - appSecret: supply this manually if you dont want it in the info.plist |
| 97 | + /// - enableDebug: Should start the Services even for Debug, Default is false |
| 98 | + /// - isDistributionEnabled: Should start the Distribution Service, Default is false for Debug and Live apps, else true, |
| 99 | + init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil) { |
| 100 | + |
| 101 | + let appSecretFromBundle = Bundle.main.object(forInfoDictionaryKey: AppCenterConstants.appSecretKey) as? String |
| 102 | + |
| 103 | + guard let _appSecret = (appSecret ?? appSecretFromBundle) else { |
| 104 | + fatalError("You have to set the `\(AppCenterConstants.appSecretKey)` key in the info plist or specify your own when initializing the SDK.") |
| 105 | + } |
| 106 | + |
| 107 | + self.enableDebug = enableDebug |
| 108 | + self.appSecret = _appSecret |
| 109 | + |
| 110 | + #if DEBUG |
| 111 | + self.isDistributionEnabled = isDistributionEnabled ?? false |
| 112 | + #elseif LIVE |
| 113 | + self.isDistributionEnabled = isDistributionEnabled ?? false |
| 114 | + #else |
| 115 | + self.isDistributionEnabled = isDistributionEnabled ?? true |
| 116 | + #endif |
| 117 | + } |
| 118 | + |
| 119 | + static var `default`: AppCenterSDK.Configuration { |
| 120 | + return Configuration() |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments