Vijay Kumar Vijay Kumar - 5 months ago 20
iOS Question

How to identify custom classes vs Framework Classes in Objective-C

I am working on an Analytics Project by Swizzling

UIViewController
methods
viewDidAppear
and
viewDidAppear
, code snippet as follows,

- (void) swizzledViewDidAppear : (BOOL)animated {
if ([UA isAppInitialized]) { // Check if Analytics Initialized
if ([[self class] isSubclassOfClass:[UIViewController class]]) {
[UA startPage:[NSString stringWithFormat:@"%@", NSStringFromClass ([self class])]];
}
}
[self swizzledViewDidAppear:animated];
}

- (void) swizzledViewDidDisappear : (BOOL)animated {
if ([UA isAppInitialized]) { // Check if Analytics Initialized
if ([[self class] isSubclassOfClass:[UIViewController class]]) {
[UA endPage:[NSString stringWithFormat:@"%@", NSStringFromClass ([self class])]];
}
}
[self swizzledViewDidDisappear:animated];
}


This is the code snippet where I want to track only the Custom ViewController, ex: MyViewController or FooViewController...etc and not Framework related classes like UICompatibilityInputViewController, UIInputWindowController...etc.

Please let me know how can I achieve this. I tried to check for Subclass but still at one point Framework classes are getting recorded.

Thanks,
Vijay

Answer

There is no way to know which are Framework classes and Custom Classes, so only way is to ignore specific classes. Ex:

ignoreClasses = [[NSArray alloc] initWithObjects:@"UICompatibilityInputViewController",
                                                    @"UIAlertController",
                                                    @"UIInputWindowController",
                                                    @"UINavigationController",
                                                    @"UIKeyboardCandidateGridCollectionViewController", nil];

.....

- (BOOL) shouldIgnoreTouch {
    for (NSString *class in ignoreClasses) {
        if ([self isKindOfClass:objc_getClass([class UTF8String])]) {
            return YES;
        }
    }
    return NO;
}

- (void) swizzledViewDidAppear : (BOOL)animated {
    if ([UA isAppInitialized]) { // Check if Analytics Initialized
        if ([[self class] isSubclassOfClass:[UIViewController class]]) {
            if (![self shouldIgnoreTouch])
              [UA startPage:[NSString stringWithFormat:@"%@",  NSStringFromClass ([self class])]];
        }
    }
    [self swizzledViewDidAppear:animated];
}

so, this is working fine and ignores these classes....

Comments