如何在Objective-C中识别自定义类与框架类

时间:2016-06-06 13:43:51

标签: ios objective-c

我正在通过Swizzling UIViewController方法viewDidAppearviewDidAppear开发Google Analytics项目,代码段如下,

- (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];
}

这是我想要仅跟踪自定义ViewController的代码片段,例如:MyViewController或FooViewController ...等,而不是像UICompatibilityInputViewController,UIInputWindowController等等与Framework相关的类。

请让我知道如何实现这一目标。我试图检查Subclass但仍然在某一点上框架类正在被记录。

谢谢, 维杰

3 个答案:

答案 0 :(得分:1)

在这种情况下,你可以像你的类一样制作实例并将其检查为

MyViewController *myVcntrl = [[MyViewController alloc]init];
    if ([myVcntrl isKindOfClass:[MyViewController class]]) {
NSLog(@"Class is of MyViewController type");
    }
else {
NSLog(@"Not of MyViewController type");
}

答案 1 :(得分:1)

你可以获得一个类的NSBundle并检查该捆绑包是否是你的。快速示例:

#import <Foundation/Foundation.h>

@interface T : NSObject
@end

@implementation T
@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSBundle *bundleOfApp = [NSBundle mainBundle];
        NSLog(@"%@", bundleOfApp.bundlePath);

        //ours
        T *t = [T new];
        NSBundle *bundleOfT = [NSBundle bundleForClass:t.class];
        NSLog(@"%@", bundleOfT.bundlePath);

        if([bundleOfT.bundlePath hasPrefix:bundleOfApp.bundlePath]) {
            NSLog(@"ours");
        }

        //theirs

        bundleOfT = [NSBundle bundleForClass:NSString.class];
        NSLog(@"%@", bundleOfT.bundlePath);

        if([bundleOfT.bundlePath hasPrefix:bundleOfApp.bundlePath]) {
        }
        else {
            NSLog(@"theirs");
        }

    }
}

答案 2 :(得分:0)

我正在调查viewDidLoad,loadView和awakeFromNib以测量加载时间,并且我注意到了一些奇怪的行为。

对于像UIInputWindowController这样的框架类,UIKeyboardCandidateGridCollectionViewController:awakeFromNib或loadView没有被调用。它们会在调用viewDidLoad后立即出现。

以下代码行:

- (void)swizzled_awakeFromNib
{
    NSLog(@"*** Awake from nib %@", NSStringFromClass(self.class));

    [self swizzled_awakeFromNib];
}

- (void)swizzled_viewDidLoad
{
    NSLog(@"*** View did load %@", NSStringFromClass(self.class));

    [self swizzled_viewDidLoad];
}

- (void)swizzled_loadView
{
    NSLog(@"*** Load view %@", NSStringFromClass(self.class));

    [self swizzled_loadView];
}

打印:

*** Awake from nib UINavigationController
*** View did load UINavigationController
*** Load view MyLoginViewController
*** View did load UIInputWindowController
*** View did load UIKeyboardCandidateGridCollectionViewController
*** View did load UIInputWindowController
*** View did load UIApplicationRotationFollowingControllerNoTouches
*** View did load MyLoginViewController

因此,如果awakeFromNib和/或loadView已调用,您可以在类别中添加属性并将其设置为YES。然后在viewDidLoad方法中检查bool以查看它是否为框架类。