Skip to content

Commit 7f1cb5b

Browse files
Updated AppCenter.swift according to HiDrive
1 parent 573eede commit 7f1cb5b

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
MSCrashes.setEnabled(HDManager.shared.settings.crashReportsEnabled)
58+
}
59+
60+
/// Returns True, if and only if the Service got started and is enabled.
61+
var isDistributionEnabled: Bool {
62+
return MSDistribute.isEnabled()
63+
}
64+
65+
/// Will enable or disable the Distribution Feature of AppCenter
66+
/// While disabling works always.
67+
/// For enabling the Setup (aka. start) - Method should be called before.
68+
///
69+
/// Flow in the App, for Apps that want to dynamically change that State:
70+
/// - Call the Setup Method for with `isDistributionEnabled` set to true.
71+
/// - Disable Distribution using `enableDistribution(enabled: false)`
72+
/// - Enable Distribution at a later time using the same method
73+
///
74+
/// - Parameter enabled: Enable or Disable Distribtion
75+
func enableDistribution(enabled: Bool = true) {
76+
MSDistribute.setEnabled(enabled)
77+
}
78+
79+
/// This will create a `fatalError` to crash the app.
80+
static func performTestCrash() {
81+
82+
MSCrashes.generateTestCrash()
83+
}
84+
85+
static let uiTestModeArgument = "SMF.UITestMode"
86+
}
87+
88+
extension AppCenterSDK {
89+
90+
struct Configuration {
91+
92+
fileprivate var enableDebug : Bool
93+
fileprivate var appSecret : String
94+
fileprivate var isDistributionEnabled : Bool
95+
96+
/// Initializes a AppCenterSDK Configuration
97+
///
98+
/// - Parameters:
99+
/// - appSecret: supply this manually if you dont want it in the info.plist
100+
/// - enableDebug: Should start the Services even for Debug, Default is false
101+
/// - isDistributionEnabled: Should start the Distribution Service, Default is false for Debug and Live apps, else true,
102+
init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil) {
103+
104+
let appSecretFromBundle = Bundle.main.object(forInfoDictionaryKey: AppCenterConstants.appSecretKey) as? String
105+
106+
guard let _appSecret = (appSecret ?? appSecretFromBundle) else {
107+
fatalError("You have to set the `\(AppCenterConstants.appSecretKey)` key in the info plist or specify your own when initializing the SDK.")
108+
}
109+
110+
self.enableDebug = enableDebug
111+
self.appSecret = _appSecret
112+
113+
#if DEBUG
114+
self.isDistributionEnabled = isDistributionEnabled ?? false
115+
#elseif LIVE
116+
self.isDistributionEnabled = isDistributionEnabled ?? false
117+
#else
118+
self.isDistributionEnabled = isDistributionEnabled ?? true
119+
#endif
120+
}
121+
122+
static var `default`: AppCenterSDK.Configuration {
123+
return Configuration()
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)