Skip to content

Commit

Permalink
Merge pull request #3464 from wordpress-mobile/issue/app-delegate-cra…
Browse files Browse the repository at this point in the history
…shlytics

Modularizes the app delegate's crashlytic code
  • Loading branch information
diegoreymendez committed Mar 27, 2015
2 parents cb273f8 + fca1334 commit 5760fc0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 39 deletions.
47 changes: 8 additions & 39 deletions WordPress/Classes/System/WordPressAppDelegate.m
Expand Up @@ -30,6 +30,7 @@
#import "ReaderTopicService.h"
#import "SVProgressHUD.h"
#import "TodayExtensionService.h"
#import "WPCrashlytics.h"

#import "WPTabBarController.h"
#import "BlogListViewController.h"
Expand Down Expand Up @@ -70,9 +71,10 @@
int ddLogLevel = LOG_LEVEL_INFO;
static NSString * const MustShowWhatsNewPopup = @"MustShowWhatsNewPopup";

@interface WordPressAppDelegate () <UITabBarControllerDelegate, CrashlyticsDelegate, UIAlertViewDelegate, BITHockeyManagerDelegate>
@interface WordPressAppDelegate () <UITabBarControllerDelegate, UIAlertViewDelegate, BITHockeyManagerDelegate>

@property (nonatomic, strong, readwrite) WPAppAnalytics *analytics;
@property (nonatomic, strong, readwrite) WPCrashlytics *crashlytics;
@property (nonatomic, strong, readwrite) WPLogger *logger;
@property (nonatomic, strong, readwrite) Reachability *internetReachability;
@property (nonatomic, strong, readwrite) Simperium *simperium;
Expand Down Expand Up @@ -645,50 +647,19 @@ - (void)changeCurrentDirectory
[fileManager changeCurrentDirectoryPath:currentDirectoryPath];
}

#pragma mark - Crash reporting
#pragma mark - Crashlytics configuration

- (void)configureCrashlytics
{
#if defined(INTERNAL_BUILD) || defined(DEBUG)
return;
#endif

if ([[WordPressComApiCredentials crashlyticsApiKey] length] == 0) {
return;
}

[Crashlytics startWithAPIKey:[WordPressComApiCredentials crashlyticsApiKey]];
[[Crashlytics sharedInstance] setDelegate:self];

[self setCommonCrashlyticsParameters];
}

- (void)crashlytics:(Crashlytics *)crashlytics didDetectCrashDuringPreviousExecution:(id<CLSCrashReport>)crash
{
DDLogMethod();
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger crashCount = [defaults integerForKey:@"crashCount"];
crashCount += 1;
[defaults setInteger:crashCount forKey:@"crashCount"];
[defaults synchronize];
}

- (void)setCommonCrashlyticsParameters
{
#if defined(INTERNAL_BUILD) || defined(DEBUG)
return;
#endif
NSString* apiKey = [WordPressComApiCredentials crashlyticsApiKey];

NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext];
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context];
BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:context];
WPAccount *defaultAccount = [accountService defaultWordPressComAccount];

BOOL loggedIn = defaultAccount != nil;
[Crashlytics setUserName:defaultAccount.username];
[Crashlytics setObjectValue:@(loggedIn) forKey:@"logged_in"];
[Crashlytics setObjectValue:@(loggedIn) forKey:@"connected_to_dotcom"];
[Crashlytics setObjectValue:@([blogService blogCountForAllAccounts]) forKey:@"number_of_blogs"];
if (apiKey) {
self.crashlytics = [[WPCrashlytics alloc] initWithAPIKey:apiKey];
}
}

- (void)configureHockeySDK
Expand Down Expand Up @@ -872,7 +843,6 @@ - (NSString *)notificationsBucketNameFromLaunchOptions:(NSDictionary *)launchOpt
return name ?: WPNotificationsBucketName;
}


#pragma mark - Keychain

+ (void)fixKeychainAccess
Expand Down Expand Up @@ -1068,7 +1038,6 @@ - (void)handleDefaultAccountChangedNote:(NSNotification *)notification
}

[self toggleExtraDebuggingIfNeeded];
[self setCommonCrashlyticsParameters];
[self setupSingleSignOn];

[WPAnalytics track:WPAnalyticsStatDefaultAccountChanged];
Expand Down
11 changes: 11 additions & 0 deletions WordPress/Classes/Utility/WPCrashlytics.h
@@ -0,0 +1,11 @@
#import <Foundation/Foundation.h>

/**
* @class WPCrashlytics
* @brief This module contains the crashlytics logic for WPiOS.
*/
@interface WPCrashlytics : NSObject

- (instancetype)initWithAPIKey:(NSString *)apiKey;

@end
94 changes: 94 additions & 0 deletions WordPress/Classes/Utility/WPCrashlytics.m
@@ -0,0 +1,94 @@
#import "WPCrashlytics.h"

#import <Crashlytics/Crashlytics.h>
#import "AccountService.h"
#import "BlogService.h"
#import "ContextManager.h"
#import "WPAccount.h"

@interface WPCrashlytics () <CrashlyticsDelegate>
@end

@implementation WPCrashlytics

#pragma mark - Dealloc

- (void)dealloc
{
[self stopObservingNotifications];
}

#pragma mark - Initialization

- (instancetype)initWithAPIKey:(NSString *)apiKey
{
NSParameterAssert(apiKey);

self = [super init];

if (self) {
[Crashlytics startWithAPIKey:apiKey];
[[Crashlytics sharedInstance] setDelegate:self];

[self setCommonCrashlyticsParameters];

[self startObservingNotifications];
}

return self;
}

#pragma mark - Notifications

- (void)startObservingNotifications
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter addObserver:self
selector:@selector(handleDefaultAccountChangedNotification:)
name:WPAccountDefaultWordPressComAccountChangedNotification
object:nil];
}

- (void)stopObservingNotifications
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter removeObserver:self];
}

- (void)handleDefaultAccountChangedNotification:(NSNotification *)notification
{
[self setCommonCrashlyticsParameters];
}

#pragma mark - Common crashlytics parameters

- (void)setCommonCrashlyticsParameters
{
NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext];
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context];
BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:context];
WPAccount *defaultAccount = [accountService defaultWordPressComAccount];

BOOL loggedIn = defaultAccount != nil;
[Crashlytics setUserName:defaultAccount.username];
[Crashlytics setObjectValue:@(loggedIn) forKey:@"logged_in"];
[Crashlytics setObjectValue:@(loggedIn) forKey:@"connected_to_dotcom"];
[Crashlytics setObjectValue:@([blogService blogCountForAllAccounts]) forKey:@"number_of_blogs"];
}

#pragma mark - CrashlyticsDelegate

- (void)crashlytics:(Crashlytics *)crashlytics didDetectCrashDuringPreviousExecution:(id<CLSCrashReport>)crash
{
DDLogMethod();
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger crashCount = [defaults integerForKey:@"crashCount"];
crashCount += 1;
[defaults setInteger:crashCount forKey:@"crashCount"];
[defaults synchronize];
}


@end
6 changes: 6 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Expand Up @@ -73,6 +73,7 @@
5903AE1B19B60A98009D5354 /* WPButtonForNavigationBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 5903AE1A19B60A98009D5354 /* WPButtonForNavigationBar.m */; };
591A428C1A6DC1B0003807A6 /* WPBackgroundDimmerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 591A428B1A6DC1B0003807A6 /* WPBackgroundDimmerView.m */; };
591A428F1A6DC6F2003807A6 /* WPGUIConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 591A428E1A6DC6F2003807A6 /* WPGUIConstants.m */; };
5926E1E31AC4468300964783 /* WPCrashlytics.m in Sources */ = {isa = PBXBuildFile; fileRef = 5926E1E21AC4468300964783 /* WPCrashlytics.m */; };
5948AD0E1AB734F2006E8882 /* WPAppAnalytics.m in Sources */ = {isa = PBXBuildFile; fileRef = 5948AD0D1AB734F2006E8882 /* WPAppAnalytics.m */; };
5948AD111AB73D19006E8882 /* WPAppAnalyticsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5948AD101AB73D19006E8882 /* WPAppAnalyticsTests.m */; };
594DB2951AB891A200E2E456 /* WPUserAgent.m in Sources */ = {isa = PBXBuildFile; fileRef = 594DB2941AB891A200E2E456 /* WPUserAgent.m */; };
Expand Down Expand Up @@ -626,6 +627,8 @@
591A428B1A6DC1B0003807A6 /* WPBackgroundDimmerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPBackgroundDimmerView.m; sourceTree = "<group>"; };
591A428D1A6DC6F2003807A6 /* WPGUIConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPGUIConstants.h; sourceTree = "<group>"; };
591A428E1A6DC6F2003807A6 /* WPGUIConstants.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPGUIConstants.m; sourceTree = "<group>"; };
5926E1E11AC4468300964783 /* WPCrashlytics.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPCrashlytics.h; sourceTree = "<group>"; };
5926E1E21AC4468300964783 /* WPCrashlytics.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPCrashlytics.m; sourceTree = "<group>"; };
5948AD0C1AB734F2006E8882 /* WPAppAnalytics.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPAppAnalytics.h; sourceTree = "<group>"; };
5948AD0D1AB734F2006E8882 /* WPAppAnalytics.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPAppAnalytics.m; sourceTree = "<group>"; };
5948AD101AB73D19006E8882 /* WPAppAnalyticsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPAppAnalyticsTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2182,6 +2185,8 @@
8525398A171761D9003F6B32 /* WPComLanguages.m */,
E183BD7217621D85000B0822 /* WPCookie.h */,
E183BD7317621D86000B0822 /* WPCookie.m */,
5926E1E11AC4468300964783 /* WPCrashlytics.h */,
5926E1E21AC4468300964783 /* WPCrashlytics.m */,
E114D798153D85A800984182 /* WPError.h */,
E114D799153D85A800984182 /* WPError.m */,
5DA3EE0E192508F700294E0B /* WPImageOptimizer.h */,
Expand Down Expand Up @@ -3510,6 +3515,7 @@
E1D04D8419374F2C002FADD7 /* BlogServiceRemoteREST.m in Sources */,
A25EBD87156E330600530E3D /* WPTableViewController.m in Sources */,
5DEB61B4156FCD3400242C35 /* WPWebView.m in Sources */,
5926E1E31AC4468300964783 /* WPCrashlytics.m in Sources */,
B55853F31962337500FAF6C3 /* NSScanner+Helpers.m in Sources */,
5D49B03B19BE3CAD00703A9B /* SafeReaderTopicToReaderTopic.m in Sources */,
5DEB61B8156FCD5200242C35 /* WPChromelessWebViewController.m in Sources */,
Expand Down

0 comments on commit 5760fc0

Please sign in to comment.