Skip to content

Commit c56ddf3

Browse files
author
UrsKahmann
committed
Added AppCenterSDK file for mac (removed distribution features)
1 parent b3001e3 commit c56ddf3

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

AppCenter/AppCenterSDK+Mac.swift

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

0 commit comments

Comments
 (0)