How to use and test MetricKit in a MacOS objective-c app?

424 Views Asked by At

I am trying to incorporate MetricKit into an empty application created for MacOS objective-C and I'm wondering how to use MetricKit. I'm following this tutorial https://nshipster.com/metrickit/. So far I have:

#import <MetricKit/MetricKit.h>
#import "AppDelegate.h"

@interface AppDelegate ()

@property (strong) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    [MXMetricManager.sharedManager addSubscriber:self];

}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
    [MXMetricManager.sharedManager removeSubscriber:self];
}


- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
    return YES;
}


@end

But I do not know if this is correct and I also do not know how to implement the didReceive method.

  1. How do I use MetricKit in objective-c?
  2. How do I test it? Reports are supposed to be sent every 24hrs, so how do I make the application receive a report?
1

There are 1 best solutions below

0
aaronsti On

You need to "conform to the protocol" of MXMetricManagerSubscriber.

Then, add the didReceiveMetricPayloads function.

#import <MetricKit/MetricKit.h>
#import "AppDelegate.h"

@interface AppDelegate () <MXMetricManagerSubscriber>
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [MXMetricManager.sharedManager addSubscriber:self];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [MXMetricManager.sharedManager removeSubscriber:self];
}

- (void)didReceiveMetricPayloads:(NSArray<MXMetricPayload *> *)payloads
{
    NSLog(@"didReceiveMetricPayloads:\n");
}

@end

To test, run your app on a real iOS device. Then, from the Xcode Debug menu, select "Simulate MetricKit Payloads"