Skip to content

Commit b3001e3

Browse files
authored
Merge pull request #94 from smartmobilefactory/Appcenter_Submodule_Update
Updated AppCenter.swift according to HiDrive
2 parents 573eede + 9308cc1 commit b3001e3

2 files changed

Lines changed: 186 additions & 7 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
import SMFLogger
14+
15+
16+
fileprivate enum AppCenterConstants {
17+
18+
static let appSecretKey = "AppCenterAppSecret"
19+
static let smfLogUploadMaxSize : Int = 5000
20+
static let crashLogFileName = "SMFLogger.log"
21+
}
22+
23+
class AppCenterSDK: NSObject {
24+
25+
// MARK: - Private static properties
26+
27+
fileprivate static let isDebugBuild : Bool = {
28+
#if DEBUG
29+
return true
30+
#else
31+
return false
32+
#endif
33+
}()
34+
35+
// MARK: - Private properties
36+
37+
fileprivate static var delegate : AppCenterSDKDelegate?
38+
39+
// MARK: - Public properties
40+
41+
static var wasInitialized : Bool {
42+
return MSAppCenter.isConfigured()
43+
}
44+
45+
// MARK: - Methods
46+
47+
/// This will setup the AppCenterSDK with the common base configuration. Crashes will be detected if the app is build with the release build type.
48+
/// Distribution can be enabled using the configuration
49+
///
50+
/// - Parameters:
51+
/// - configuration: Configuration object
52+
static func setup(configuration: AppCenterSDK.Configuration = .default) {
53+
guard (self.isDebugBuild == false || configuration.enableDebug == true) else {
54+
return
55+
}
56+
57+
self.delegate = AppCenterSDKDelegate(isLogUploadEnabled: configuration.isLogUploadEnabled)
58+
59+
let services = (configuration.isDistributionEnabled == true) ? [MSCrashes.self, MSDistribute.self] : [MSCrashes.self]
60+
61+
MSAppCenter.start(configuration.appSecret, withServices: services)
62+
MSCrashes.setEnabled(configuration.isCrashReportEnabled)
63+
MSCrashes.setDelegate(self.delegate)
64+
}
65+
66+
/// Returns True, if and only if the Service got started and is enabled.
67+
static var isDistributionEnabled: Bool {
68+
return MSDistribute.isEnabled()
69+
}
70+
71+
/// Will enable or disable the Distribution Feature of AppCenter
72+
/// While disabling works always.
73+
/// For enabling the Setup (aka. start) - Method should be called before.
74+
///
75+
/// Flow in the App, for Apps that want to dynamically change that State:
76+
/// - Call the Setup Method for with `isDistributionEnabled` set to true.
77+
/// - Disable Distribution using `enableDistribution(enabled: false)`
78+
/// - Enable Distribution at a later time using the same method
79+
///
80+
/// - Parameter enabled: Enable or Disable Distribtion
81+
static func enableDistribution(enabled: Bool = true) {
82+
MSDistribute.setEnabled(enabled)
83+
}
84+
85+
/// Returns True, if and only if Crash Reporting is enabled.
86+
static var isCrashReportingEnabled: Bool {
87+
return MSCrashes.isEnabled()
88+
}
89+
90+
/// Will enable or disable the sending od crash reports to AppCenter
91+
/// While disabling works always.
92+
/// For enabling the Setup (aka. start) - Method should be called before.
93+
///
94+
/// Flow in the App, for Apps that want to dynamically change that State:
95+
/// - Call the Setup Method for with `isDistributionEnabled` set to true.
96+
/// - Disable Distribution using `enableDistribution(enabled: false)`
97+
/// - Enable Distribution at a later time using the same method
98+
///
99+
/// - Parameter enabled: Enable or Disable Distribtion
100+
static func enableCrashReporting(enabled: Bool = true) {
101+
MSCrashes.setEnabled(enabled)
102+
}
103+
104+
/// This will create a `fatalError` to crash the app.
105+
static func performTestCrash() {
106+
107+
MSCrashes.generateTestCrash()
108+
}
109+
}
110+
111+
extension AppCenterSDK {
112+
113+
struct Configuration {
114+
115+
fileprivate var enableDebug : Bool
116+
fileprivate var appSecret : String
117+
fileprivate var isDistributionEnabled : Bool
118+
fileprivate var isCrashReportEnabled : Bool
119+
fileprivate var isLogUploadEnabled : Bool
120+
121+
/// Initializes a AppCenterSDK Configuration
122+
///
123+
/// - Parameters:
124+
/// - appSecret: supply this manually if you dont want it in the info.plist
125+
/// - enableDebug: Should start the Services even for Debug, Default is false
126+
/// - isDistributionEnabled: Should start the Distribution Service, Default is false for Debug and Live apps, else true
127+
/// - isCrashReportEnabled: enables sending of crash reports, Default is true
128+
/// - isLogUploadEnabled: enables attachment of logs to crash reports, Default is true
129+
init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil, isCrashReportEnabled: Bool = true, isLogUploadEnabled: Bool = true) {
130+
131+
let appSecretFromBundle = Bundle.main.object(forInfoDictionaryKey: AppCenterConstants.appSecretKey) as? String
132+
133+
guard let _appSecret = (appSecret ?? appSecretFromBundle) else {
134+
fatalError("You have to set the `\(AppCenterConstants.appSecretKey)` key in the info plist or specify your own when initializing the SDK.")
135+
}
136+
137+
self.enableDebug = enableDebug
138+
self.appSecret = _appSecret
139+
self.isCrashReportEnabled = isCrashReportEnabled
140+
self.isLogUploadEnabled = isLogUploadEnabled
141+
142+
#if DEBUG
143+
self.isDistributionEnabled = isDistributionEnabled ?? false
144+
#elseif LIVE
145+
self.isDistributionEnabled = isDistributionEnabled ?? false
146+
#else
147+
self.isDistributionEnabled = isDistributionEnabled ?? true
148+
#endif
149+
}
150+
151+
static var `default`: AppCenterSDK.Configuration {
152+
return Configuration()
153+
}
154+
}
155+
}
156+
157+
private class AppCenterSDKDelegate : NSObject, MSCrashesDelegate {
158+
159+
private var isLogUploadEnabled : Bool
160+
161+
init(isLogUploadEnabled : Bool) {
162+
self.isLogUploadEnabled = isLogUploadEnabled
163+
}
164+
165+
func attachments(with crashes: MSCrashes!, for errorReport: MSErrorReport!) -> [MSErrorAttachmentLog]! {
166+
guard (self.isLogUploadEnabled == true) else {
167+
return []
168+
}
169+
170+
return [MSErrorAttachmentLog.attachment(withText: self.applicationLog, filename: AppCenterConstants.crashLogFileName)]
171+
}
172+
173+
private var applicationLog: String {
174+
guard
175+
(self.isLogUploadEnabled == true),
176+
let description = Logger.logFilesContent(maxSize: AppCenterConstants.smfLogUploadMaxSize),
177+
(description.isEmpty == false) else {
178+
return "No Log found"
179+
}
180+
181+
return description
182+
}
183+
}
184+

AppCenter/AppCenterSDK+SMFLogger.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class AppCenterSDK: NSObject {
2828
#endif
2929
}()
3030

31-
// MARK: - Private properties
32-
33-
fileprivate var isInitialized = false
34-
fileprivate var configuration : Configuration?
35-
3631
// MARK: - Public properties
3732

3833
static var wasInitialized : Bool {
@@ -57,7 +52,7 @@ class AppCenterSDK: NSObject {
5752
}
5853

5954
/// Returns True, if and only if the Service got started and is enabled.
60-
var isDistributionEnabled: Bool {
55+
static var isDistributionEnabled: Bool {
6156
return MSDistribute.isEnabled()
6257
}
6358

@@ -71,7 +66,7 @@ class AppCenterSDK: NSObject {
7166
/// - Enable Distribution at a later time using the same method
7267
///
7368
/// - Parameter enabled: Enable or Disable Distribtion
74-
func enableDistribution(enabled: Bool = true) {
69+
static func enableDistribution(enabled: Bool = true) {
7570
MSDistribute.setEnabled(enabled)
7671
}
7772

0 commit comments

Comments
 (0)