Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Thumbtack's Crashlytics Adapter
#import <Foundation/Foundation.h>
/* How To Enable CrashlyticsAdapter
*
* # On the App Target
*
* Initialize Crashlytics as usual and pass the real Crashlytics instance to the adapter:
*
* [Fabric with:@[[Crashlytics class]]];
* [CrashlyticsAdapter with:[Crashlytics sharedInstance]];
*
* # On Libraries That Need Crashlytics
*
* Just use `[CrashlyticsAdapter sharedInstance]` instead of `CrashlyticsKit` or `[Crashlytics sharedInstance]`.
*
*/
@protocol CrashlyticsAPI
- (void)setUserIdentifier:(NSString *)identifier;
- (void)setObjectValue:(NSObject *)value forKey:(NSString *)key;
- (void)recordError:(NSError *)error;
- (void)recordError:(NSError *)error withAdditionalUserInfo:(NSDictionary<NSString *, NSObject *> *)userInfo;
@end
@interface CrashlyticsAdapter : NSObject <CrashlyticsAPI>
+ (instancetype)sharedInstance;
+ (void)with:(id<CrashlyticsAPI>)crashlytics;
- (instancetype)init NS_UNAVAILABLE;
@end
#import "CrashlyticsAdapter.h"
@interface CrashlyticsAdapter ()
@property (nonatomic, strong) id<CrashlyticsAPI> crashlytics;
@end
@implementation CrashlyticsAdapter
static CrashlyticsAdapter *sharedInstance;
+ (instancetype)sharedInstance {
return sharedInstance;
}
+ (void)with:(id<CrashlyticsAPI>)crashlytics {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[CrashlyticsAdapter alloc] initWithCrashlytics:crashlytics];
});
}
- (instancetype)initWithCrashlytics:(id<CrashlyticsAPI>)crashlytics {
self = [super init];
if (self) {
self.crashlytics = crashlytics;
}
return self;
}
#pragma mark - Crashlytics API Delegation
- (void)setUserIdentifier:(NSString *)identifier {
[self.crashlytics setUserIdentifier:identifier];
}
- (void)setObjectValue:(NSObject *)value forKey:(NSString *)key {
[self.crashlytics setObjectValue:value forKey:key];
}
- (void)recordError:(NSError *)error {
[self.crashlytics recordError:error];
}
- (void)recordError:(NSError *)error withAdditionalUserInfo:(NSDictionary<NSString *, NSObject *> *)userInfo {
[self.crashlytics recordError:error withAdditionalUserInfo:userInfo];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment