FIRCrashlytics.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import "FIRCrashlyticsReport.h"
  16. #import "FIRExceptionModel.h"
  17. #if __has_include(<Crashlytics/Crashlytics.h>)
  18. #warning "FirebaseCrashlytics and Crashlytics are not compatible \
  19. in the same app because including multiple crash reporters can \
  20. cause problems when registering exception handlers."
  21. #endif
  22. NS_ASSUME_NONNULL_BEGIN
  23. /**
  24. * The Firebase Crashlytics API provides methods to annotate and manage fatal and
  25. * non-fatal reports captured and reported to Firebase Crashlytics.
  26. *
  27. * By default, Firebase Crashlytics is initialized with `FirebaseApp.configure()`.
  28. *
  29. * Note: The Crashlytics class cannot be subclassed. If this makes testing difficult,
  30. * we suggest using a wrapper class or a protocol extension.
  31. */
  32. NS_SWIFT_NAME(Crashlytics)
  33. @interface FIRCrashlytics : NSObject
  34. /** :nodoc: */
  35. - (instancetype)init NS_UNAVAILABLE;
  36. /**
  37. * Accesses the singleton Crashlytics instance.
  38. *
  39. * @return The singleton Crashlytics instance.
  40. */
  41. + (instancetype)crashlytics NS_SWIFT_NAME(crashlytics());
  42. /**
  43. * Adds logging that is sent with your crash data. The logging does not appear in app
  44. * logs and is only visible in the Crashlytics dashboard.
  45. *
  46. * @param msg Message to log
  47. */
  48. - (void)log:(NSString *)msg;
  49. /**
  50. * Adds logging that is sent with your crash data. The logging does not appear in app
  51. * logs and is only visible in the Crashlytics dashboard.
  52. *
  53. * @param format Format of string
  54. * @param ... A comma-separated list of arguments to substitute into format
  55. */
  56. - (void)logWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
  57. /**
  58. * Adds logging that is sent with your crash data. The logging does not appear in app
  59. * logs and is only visible in the Crashlytics dashboard.
  60. *
  61. * @param format Format of string
  62. * @param args Arguments to substitute into format
  63. */
  64. - (void)logWithFormat:(NSString *)format
  65. arguments:(va_list)args
  66. __attribute__((__swift_name__("log(format:arguments:)"))); // Avoid `NS_SWIFT_NAME` (#9331).
  67. /**
  68. * Sets a custom key and value to be associated with subsequent fatal and non-fatal reports.
  69. * When setting an object value, the object is converted to a string. This is
  70. * typically done by using the object's `description`.
  71. *
  72. * @param value The value to be associated with the key
  73. * @param key A unique key
  74. */
  75. - (void)setCustomValue:(nullable id)value forKey:(NSString *)key;
  76. /**
  77. * Sets custom keys and values to be associated with subsequent fatal and non-fatal reports.
  78. * The objects in the dictionary are converted to strings. This is
  79. * typically done by using the object's `description`.
  80. *
  81. * @param keysAndValues The values to be associated with the corresponding keys
  82. */
  83. - (void)setCustomKeysAndValues:(NSDictionary *)keysAndValues;
  84. /**
  85. * Records a user ID (identifier) that's associated with subsequent fatal and non-fatal reports.
  86. *
  87. * If you want to associate a crash with a specific user, we recommend specifying an arbitrary
  88. * string (e.g., a database, ID, hash, or other value that you can index and query, but is
  89. * meaningless to a third-party observer). This allows you to facilitate responses for support
  90. * requests and reach out to users for more information.
  91. *
  92. * @param userID An arbitrary user identifier string that associates a user to a record in your
  93. * system.
  94. */
  95. - (void)setUserID:(nullable NSString *)userID;
  96. /**
  97. * Records a non-fatal event described by an Error object. The events are
  98. * grouped and displayed similarly to crashes. Keep in mind that this method can be expensive.
  99. * The total number of Errors that can be recorded during your app's life-cycle is limited by a
  100. * fixed-size circular buffer. If the buffer is overrun, the oldest data is dropped. Errors are
  101. * relayed to Crashlytics on a subsequent launch of your application.
  102. *
  103. * @param error Non-fatal error to be recorded
  104. */
  105. - (void)recordError:(NSError *)error NS_SWIFT_NAME(record(error:));
  106. /**
  107. * Records a non-fatal event described by an NSError object. The events are
  108. * grouped and displayed similarly to crashes. Keep in mind that this method can be expensive.
  109. * The total number of NSErrors that can be recorded during your app's life-cycle is limited by a
  110. * fixed-size circular buffer. If the buffer is overrun, the oldest data is dropped. Errors are
  111. * relayed to Crashlytics on a subsequent launch of your application.
  112. *
  113. * @param error Non-fatal error to be recorded
  114. * @param userInfo Additional keys and values to send with the logged error. These parameters are
  115. * added to Crashlytics global list of keys and values that live with the session.
  116. */
  117. - (void)recordError:(NSError *)error
  118. userInfo:(nullable NSDictionary<NSString *, id> *)userInfo
  119. NS_SWIFT_NAME(record(error:userInfo:));
  120. /**
  121. * Records an Exception Model described by an ExceptionModel object. The events are
  122. * grouped and displayed similarly to crashes. Keep in mind that this method can be expensive.
  123. * The total number of ExceptionModels that can be recorded during your app's life-cycle is
  124. * limited by a fixed-size circular buffer. If the buffer is overrun, the oldest data is dropped.
  125. * ExceptionModels are relayed to Crashlytics on a subsequent launch of your application.
  126. *
  127. * @param exceptionModel Instance of the ExceptionModel to be recorded
  128. */
  129. - (void)recordExceptionModel:(FIRExceptionModel *)exceptionModel
  130. NS_SWIFT_NAME(record(exceptionModel:));
  131. /**
  132. * Returns whether the app crashed during the previous execution.
  133. */
  134. - (BOOL)didCrashDuringPreviousExecution;
  135. /**
  136. * Enables/disables automatic data collection.
  137. *
  138. * Calling this method overrides both the FirebaseCrashlyticsCollectionEnabled flag in your
  139. * App's Info.plist and FirebaseApp's isDataCollectionDefaultEnabled flag.
  140. *
  141. * When you set a value for this method, it persists across runs of the app.
  142. *
  143. * The value does not apply until the next run of the app. If you want to disable data
  144. * collection without rebooting, add the FirebaseCrashlyticsCollectionEnabled flag to your app's
  145. * Info.plist.
  146. * *
  147. * @param enabled Determines whether automatic data collection is enabled
  148. */
  149. - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled;
  150. /**
  151. * Indicates whether or not automatic data collection is enabled
  152. *
  153. * This method uses three ways to decide whether automatic data collection is enabled,
  154. * in order of priority:
  155. * - If setCrashlyticsCollectionEnabled is called with a value, use it
  156. * - If the FirebaseCrashlyticsCollectionEnabled key is in your app's Info.plist, use it
  157. * - Otherwise, use the default isDataCollectionDefaultEnabled in FirebaseApp
  158. */
  159. - (BOOL)isCrashlyticsCollectionEnabled;
  160. /**
  161. * Determines whether there are any unsent crash reports cached on the device, then calls the given
  162. * callback.
  163. *
  164. * The callback only executes if automatic data collection is disabled. You can use
  165. * the callback to get one-time consent from a user upon a crash, and then call
  166. * sendUnsentReports or deleteUnsentReports, depending on whether or not the user gives consent.
  167. *
  168. * Disable automatic collection by:
  169. * - Adding the `FirebaseCrashlyticsCollectionEnabled` key with the value set to NO to your app's
  170. * Info.plist
  171. * - Calling `FirebaseCrashlytics.crashlytics().setCrashlyticsCollectionEnabled(false)` in your app
  172. * - Setting `FirebaseApp`'s `isDataCollectionDefaultEnabled` to false
  173. *
  174. * @param completion The callback that's executed once Crashlytics finishes checking for unsent
  175. * reports. The callback is set to true if there are unsent reports on disk.
  176. */
  177. - (void)checkForUnsentReportsWithCompletion:(void (^)(BOOL))completion
  178. NS_SWIFT_NAME(checkForUnsentReports(completion:));
  179. /**
  180. * Determines whether there are any unsent crash reports cached on the device, then calls the given
  181. * callback with a CrashlyticsReport object that you can use to update the unsent report.
  182. * CrashlyticsReports have a lot of the familiar Crashlytics methods like setting custom keys and
  183. * logs.
  184. *
  185. * The callback only executes if automatic data collection is disabled. You can use
  186. * the callback to get one-time consent from a user upon a crash, and then call
  187. * sendUnsentReports or deleteUnsentReports, depending on whether or not the user gives consent.
  188. *
  189. * Disable automatic collection by:
  190. * - Adding the `FirebaseCrashlyticsCollectionEnabled` key with the value set to NO to your app's
  191. * Info.plist
  192. * - Calling `FirebaseCrashlytics.crashlytics().setCrashlyticsCollectionEnabled(false)` in your app
  193. * - Setting `FirebaseApp`'s `isDataCollectionDefaultEnabled` to false
  194. *
  195. * Not calling `sendUnsentReports()`/`deleteUnsentReports()` will result in the report staying on
  196. * disk, which means the same CrashlyticsReport can show up in multiple runs of the app. If you
  197. * want avoid duplicates, ensure there was a crash on the last run of the app by checking the value
  198. * of `didCrashDuringPreviousExecution`.
  199. *
  200. * @param completion The callback that's executed once Crashlytics finishes checking for unsent
  201. * reports. The callback is called with the newest unsent Crashlytics Report, or nil if there are
  202. * none cached on disk.
  203. */
  204. - (void)checkAndUpdateUnsentReportsWithCompletion:
  205. (void (^)(FIRCrashlyticsReport *_Nullable))completion
  206. NS_SWIFT_NAME(checkAndUpdateUnsentReports(completion:));
  207. /**
  208. * Enqueues any unsent reports on the device to upload to Crashlytics.
  209. *
  210. * This method only applies if automatic data collection is disabled.
  211. *
  212. * When automatic data collection is enabled, Crashlytics automatically uploads and deletes reports
  213. * at startup, so this method is ignored.
  214. */
  215. - (void)sendUnsentReports;
  216. /**
  217. * Deletes any unsent reports on the device.
  218. *
  219. * This method only applies if automatic data collection is disabled.
  220. */
  221. - (void)deleteUnsentReports;
  222. @end
  223. NS_ASSUME_NONNULL_END