Skip to content

Commit

Permalink
feat: feat: swizzled on app will appear
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyssa.Yu authored and Alyssa.Yu committed Sep 6, 2023
1 parent 478f63c commit a735137
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
1 change: 1 addition & 0 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ - (void)initializeApiKey:(NSString *)apiKey
// Unlike other default events options that can be evaluated later, screenViews has to be evaluated during the actual initialization
if (self.defaultTracking.screenViews) {
[UIViewController amp_swizzleViewDidAppear];
[UIViewController amp_swizzleViewWillAppear];
}
#endif
}];
Expand Down
1 change: 1 addition & 0 deletions Sources/Amplitude/UIViewController+AMPScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@interface UIViewController (AMPScreen)

+ (void)amp_swizzleViewDidAppear;
+ (void)amp_swizzleViewWillAppear;
+ (UIViewController *)amp_rootViewControllerFromView:(UIView *)view;
+ (UIViewController *)amp_topViewController:(UIViewController *)rootViewController;

Expand Down
97 changes: 86 additions & 11 deletions Sources/Amplitude/UIViewController+AMPScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
#import "AMPConstants.h"
#import <UIKit/UIKit.h>

static NSNotificationName const __UIWindowSceneDidChangeScreenNotification = @"__UIWindowSceneDidChangeScreenNotification";


#if !TARGET_OS_OSX && !TARGET_OS_WATCH


Expand Down Expand Up @@ -75,6 +72,35 @@ + (void)amp_swizzleViewDidAppear {
});
}

+ (void)amp_swizzleViewWillAppear {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];

SEL originalSelector = @selector(viewWillAppear:);
SEL swizzledSelector = @selector(amp_viewWillAppear:);

Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}



+ (UIViewController *)amp_rootViewControllerFromView:(UIView *)view {
UIViewController *root = view.window.rootViewController;
Expand Down Expand Up @@ -120,7 +146,8 @@ + (UIViewController *)amp_nextRootViewController:(UIViewController *)rootViewCon
return nil;
}

- (void)amp_viewDidAppear:(BOOL)animated {

- (void)amp_viewWillAppear:(BOOL)animated {
AMPLITUDE_LOG(@"self is %@", self);
UIViewController *top = [[self class] amp_rootViewControllerFromView:self.view];
if (!top) {
Expand All @@ -137,13 +164,57 @@ - (void)amp_viewDidAppear:(BOOL)animated {
name = @"Unknown";
}
}

NSLog(@"**********view will appear**********");
//NSLog(@"Screen changed!");

printViewHierarchy(self.view, 0);

/*
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0f);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
UIImage *snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/

[[Amplitude instance] logEvent:kAMPScreenViewed withEventProperties:@{
kAMPEventPropScreenName: name ?: @"",
}];

// call original method, this is not recurrsive method call
[self amp_viewWillAppear:animated];
}

- (void)amp_viewDidAppear:(BOOL)animated {
AMPLITUDE_LOG(@"self is %@", self);
UIViewController *top = [[self class] amp_rootViewControllerFromView:self.view];
if (!top) {
AMPLITUDE_LOG(@"Failed to infer screen");
return;
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenChanged:)
name:__UIWindowSceneDidChangeScreenNotification
object:nil];
NSString *name = [top title];
if (!name || name.length == 0) {
// if no class title found, try view controller's description
name = [[[top class] description] stringByReplacingOccurrencesOfString:@"ViewController" withString:@""];
if (name.length == 0) {
AMPLITUDE_LOG(@"Failed to infer screen name");
name = @"Unknown";
}
}

NSLog(@"**********View did appear**********");
//NSLog(@"Screen changed!");

printViewHierarchy(self.view, 0);

/*
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0f);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
UIImage *snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/

[[Amplitude instance] logEvent:kAMPScreenViewed withEventProperties:@{
kAMPEventPropScreenName: name ?: @"",
}];
Expand All @@ -152,12 +223,16 @@ - (void)amp_viewDidAppear:(BOOL)animated {
[self amp_viewDidAppear:animated];
}

- (void)screenChanged:(NSNotification *)notification {
/*
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenChanged:) name:@"screenChanged" object:nil];
//UIAccessibilityScreenChangedNotification
- (void)screenChanged:(NSNotification *)notification {
// Handle the screen change here
NSLog(@"**********Before print view hierarchy**********");
NSLog(@"Screen changed!");
printViewHierarchy(self.view, 0);
}
}*/

void printViewHierarchy(UIView *view, int indent) {
NSMutableString *indentation = [NSMutableString string];
Expand All @@ -170,6 +245,6 @@ void printViewHierarchy(UIView *view, int indent) {
printViewHierarchy(subview, indent + 4);
}
}

// city of staff person.
@end
#endif

0 comments on commit a735137

Please sign in to comment.