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.
How do I use MetricKit in objective-c?
How do I test it? Reports are supposed to be sent every 24hrs, so how do I make the application receive a report?
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"
Related
I need to make main window to preserve width/height ratio when the user resizes it on MacOS.
My first idea was to handle the following notification:
#interface Override_iOS : NSObject
#end
__strong Override_iOS *_instance;
#implementation Override_iOS
+(void)load
{
NSLog(#"[Override_iOS load]");
_instance = [Override_iOS new];
[[NSNotificationCenter defaultCenter] addObserver:_instance
selector:#selector(applicationDidFinishLaunching:)
name:UIApplicationDidFinishLaunchingNotification
object:nil];
}
-(void)applicationDidFinishLaunching:(NSNotification*) notification
{
NSLog(#"[Override_iOS applicationDidFinishLaunching:%#]", notification);
//qDebug() << "applicationDidFinishLaunching handler";
}
#end
and try to set something like aspectRatio property of the main window, but looks like that this code would not work on MacOS, but only on iOS (at least in my app).
Is it possible to do something like this on MacOS?
Is there an option in XCode project?
EDIT1
I need something like this:
- (void)awakeFromNib {
// Keep the aspect ratio constant at its current value
[window setAspectRatio:window.frame.size];
}
but it is not clear where to add this.
EDIT1
Created a test app:
I have done very little with MacOS apps, but this may be what you're looking for...
Create a NSWindow subclass:
MyWindowController.h
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
#interface MyWindowController : NSWindowController
#end
NS_ASSUME_NONNULL_END
MyWindowController.m
#import "MyWindowController.h"
#interface MyWindowController ()
#end
#implementation MyWindowController
- (void)windowDidLoad {
[super windowDidLoad];
// set the aspect ratio here
[self.window setAspectRatio:NSMakeSize(320.0, 640.0)];
}
#end
Then assign the custom class of your window controller:
I'm a newbie in Cocoa
I have a function in a class call TextSaver.m :
- (void) save {
TheNotes *myNote = [[TheNotes alloc]init];
myNote.theText = [theTextView string];
NSLog(#"%#",myNote.theText);
[NSKeyedArchiver archiveRootObject:myNote.theText toFile:#"..."];
}
And I'm calling it from the AppDelegate with applicationWillTerminate :
- (void)applicationWillTerminate:(NSNotification *)notification{
[theTextSaver save];
}
But NSLog(#"%#",myNote.theText); results null... Like NSLog(#"%#",theTextView);. Which means when I call the function I can't access theTextView.
I've already try to call this function in the TextSaver.m class with a -(IBAction) and it worked!
Hope you can help me !
EDIT
The TextSaver is created with an #import TextSaver.h and in the appInterface
TextSaver *theTextSaver;
EDIT 2
I rewrite the code to make it simpler :
AppDelegate.h :
#import <Cocoa/Cocoa.h>
#import "TheNotes.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>{
TheNotes *myNote;
}
#property (copy) TheNotes *myNote;
#property (assign) IBOutlet NSWindow *window;
#end
AppDelegate.m :
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize myNote;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
myNote = [[TheNotes alloc]init]; //Do I need to put it in -(id) init ?
}
- (void)applicationWillTerminate:(NSNotification *)notification{
[myNote save];
}
#end
TheNotes.h :
#import <Foundation/Foundation.h>
#interface TheNotes : NSObject {
NSString *theText;
IBOutlet NSTextView *theTextView;// Do I need to alloc memory ?
}
-(void) save;
#property (copy) NSString *theText;
#end
TheNotes.m :
#implementation TheNotes
#synthesize theText;
- (void) save {
NSLog(#"%#",theTextView.string);// Save is properly called, but this results (null).
[NSKeyedArchiver archiveRootObject:theTextView.string toFile:#"..."];
}
#end
The two questions you need to answer for yourself are:
Why do I expect my TextSaver to know about the text view?
Where do I tell the TextSaver about the text view?
The other possible answer to the first question is “the TextSaver created the text view”, but I'm assuming that's not the case.
So, you need to find where you think you're telling the TextSaver about the text view and make sure that's the case.
If you haven't done anything specific to tell the TextSaver about the text view, but rather are expecting it to just know about it, then that's probably the problem.
As Phillip Mills alluded to in his comment, merely declaring a variable named theTextView does not mean that the TextSaver knows about the text view. The compiler cannot read English: the names you choose are for your own benefit only; the compiler treats them only as identifiers. It does not see “theTextView” and go “oh, that! that's over there; I'll go get it”.
In order for theTextView to actually point to the text view, you need to put the text view there. You do this via assignment. Either expose theTextView as a property and set it from somewhere else, or set it internally within the TextSaver class (after either creating the text view yourself or getting it from another object).
I would make it a property (named simply textView) and set that property from whatever owns both the TextSaver and the text view.
This is a working example of basically what you're trying to achieve, HTH.
There are two classes, AppDelegate and TestViewController. TestViewController has a UITextView, whenever the user presses the home button of the device while editing the UITextView, the - (void)applicationDidEnterBackground:(UIApplication *)application method of the AppDelegate is called and the note is printed to the console (here you could save the note instead).
I use applicationDidEnterBackground because it's what is called when the app goes into background mode.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
PVSTestViewController *nextScreen = [[PVSTestViewController alloc] init];
self.delegate = nextScreen; // Assign TestViewController as the AppDelegate's delegate.
self.window.rootViewController = nextScreen;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.delegate saveData]; // Called when the user presses the home button.
}
AppDelegate.h
....
#protocol PVSAppDelegateProtocol <NSObject>
- (void)saveData; // Any class that conforms to our protocol must implement this method.
#end
#interface PVSAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, weak) id<PVSAppDelegateProtocol> delegate; // Here we store the delegate class.
#end
TestViewController.m
#import "PVSTestViewController.h"
#interface PVSTestViewController ()
#property (weak, nonatomic) IBOutlet UITextView *textView;
#end
#implementation PVSTestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)saveData
{
// This is called by the AppDelegate when the app goes into the background.
NSLog([NSString stringWithFormat:#"Text is: %#", self.textView.text ]);
}
#end
TestViewController.h
....
#import "PVSAppDelegate.h"
#interface PVSTestViewController : UIViewController <PVSAppDelegateProtocol>
#end
I have an app that has three views. All three views have an Ad Banner at the bottom of the screen. The first view creates an audio streamer which is paused when the Ad Banner on this view is clicked. I'm trying to use the AdBanner delegate methods on the second view to stop/start the audio. When the Ad Banner is selected the AdBanner delegate methods should call my custom delegate functions. The code compiles and runs but doesn't function correctly.
Using NSLog I've determined that the Ad Banner is calling its delegate function correctly but this isn't calling the custom delegate.
Hope this makes sense. Any help would be appreciated. Here is my code.
SecondViewControler H-file
#import "SecondViewController.h"
#protocol demoViewControllerDelegate <NSObject>
#required
-(void)stopSent;
-(void)startSent;
#end
#interface SecondViewController ()
{
id<demoViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id<demoViewControllerDelegate> delegate;
#end
SecondViewController M-file
#implementation SecondViewController
#synthesize delegate;
Protocols
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
[delegate stopSent];
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner{
[delegate startSent];
}
FirstViewController H-file
#import <QuartzCore/QuartzCore.h>
#import "iAd/iAd.h"
#import <MessageUI/MessageUI.h>
#import "AudioStreamer.h"
#import "Reachability.h"
#import "SecondViewController.h"
#import "MFAppDelegate.h"
#import "MFSideMenu.h"
Class secondViewConroller;
#interface DemoViewController : UIViewController <ADBannerViewDelegate,demoViewControllerDelegate> {
}
#end
FirstViewController M-file
-(void)stopSent{
if (isPlaying) {
[streamer stop];
wasPlaying=true;
}
}
-(void)startSent{
if (wasPlaying) {
[streamer start];
isPlaying=true;
}
}
Your protocol methods need to be implemented in the class that you've designated as your delegate target.
It looks like your DemoViewController (or FirstViewController) is the object you've designated as the delegate, since you've given the interface the "<ADBannerViewDelegate,demoViewControllerDelegate>" designations.
Then, from your Second View Controller, you can call the object you designated and set as a delegate by doing:
[delegate startSent];
and
[delegate stopSent];
in the appropriate locations, which appear to be "bannerViewActionShouldBegin" and "bannerViewActionDidFinish", respectively.
You should also make sure that the delegate is properly set, therefore instead of:
[delegate startSent];
you should actually do this:
if(delegate)
[delegate startSent];
else
NSLog( #"delegate is null; we should figure out why" );
Good day everyone,
I am not able to call the method refresh in my ViewController when using a Storyboad. However it works if I create the project without the Storyboard, just a NIB. Why it doesn't work and how to overcome this issue?
Here is my code:
AppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(#"app will enter foreground");
[viewController refresh:NULL];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
-(IBAction) refresh:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)loadData {
// contains information the ViewController makes use of
}
-(IBAction)refresh:(id) sender {
[self loadData];
}
#end
The viewController property in your app delegate are not populated when using a storyboard. Include the value in your log statement for confirmation - it will be null.
Register your view controller for the notification instead, and let it handle it's own refreshing.
In viewDidLoad:
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refresh:)
name:UIApplicationDidBecomeActiveNotification
object: nil];
You'll want to remove this observer in dealloc / viewDidUnload as well.
I've got this class.
.h:
#import
#class GLEngine;
#interface opengl_engineAppDelegate : NSObject {
// Pointer to engine
GLEngine * myGLEngine;
UIWindow * window;
}
#property (nonatomic, retain) IBOutlet GLEngine * myGLEngine;
#property (nonatomic, retain) IBOutlet UIWindow * window;
#end
Here is .m:
#import "opengl_engineAppDelegate.h"
#import "GLEngine.h"
#implementation opengl_engineAppDelegate
#synthesize window;
#synthesize myGLEngine;
// Creating Application
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[myGLEngine activateEngine];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[myGLEngine activateEngine];
}
// Destroying Application
- (void)dealloc {
[window release];
[myGLEngine release];
[super dealloc];
}
#end
And GLEngine looks like:
#import
#import
#import
#import
#import
#import
#interface GLEngine : UIView {
}
- (void)activateEngine;
#end
Why activateEngine never calls?
It looks like you are never allocing myGLEngine or setting it to anything. You should set a breakpoint at the activateEngine call and check that myGLEngine is actually the object you think it is.