#import "AppDelegate.h"
@interface AppDelegate ()
/// 顶部状态栏按钮
@property (nonatomic, strong) NSStatusItem *statusBarItem;
// 自定义的窗口控制器
//@property (nonatomic, strong) NSWindowController *mainWindowController;
@end
@implementation AppDelegate
/*
启动顺序:
[AppDelegate applicationWillFinishLaunching:]
[AppDelegate applicationWillBecomeActive:]
[AppDelegate applicationDidBecomeActive:]
[AppDelegate applicationWillUpdate:]
[AppDelegate applicationDidUpdate:]
[AppDelegate applicationDidFinishLaunching:]
[AppDelegate applicationWillUpdate:],[AppDelegate applicationDidUpdate:] // 多次触发
挂起(不会挂起进程)
[AppDelegate applicationWillResignActive:]
[AppDelegate applicationDidResignActive:]
激活
[AppDelegate applicationWillBecomeActive:]
[AppDelegate applicationDidBecomeActive:]
[AppDelegate applicationWillUpdate:],[AppDelegate applicationDidUpdate:] // 多次触发
*/
- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
// 设置屏幕顶部状态栏按钮
[self initStatusBarItem];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// 全局锁定浅色外观,在不打算适配深色外观时用到
if (@available(macOS 10.14, *)) {
[NSApplication sharedApplication].appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
}
/* 自定义显示的窗口,需要先在Main.storyboard里把"Window Controller"控件的"Is Inital Controller"选项移除,然后自定义NSWindowController实例,NSWindowController实例化详情参考《macOS - NSWindowController》
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Custom" bundle:[NSBundle mainBundle]];
NSWindowController *windowController = [storyboard instantiateInitialController];
//[windowController.window makeMainWindow]; // 需要NSWindow子类化实现-[NSWindow canBecomeMainWindow]并返回YES
[windowController showWindow:nil];
self.mainWindowController = windowController;
*/
}
#pragma mark 屏幕顶部状态栏按钮,一般用于激活窗口,如果要上架App Store,就必须加这个
- (void)initStatusBarItem
{
NSStatusItem *item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
self.statusBarItem = item;
item.button.action = @selector(statusBarItemClicked:);
item.button.target = self;
item.button.toolTip = [[NSBundle mainBundle].infoDictionary valueForKey:(__bridge NSString *)kCFBundleNameKey];
item.button.imageScaling = NSImageScaleNone;
/*
图片建议大小:16pt × 16pt
图片建议设置为模板(Assets.xcassets图片属性Renders As改为Template Image,或NSImage.template = YES)
*/
item.button.image = [NSImage imageNamed:NSImageNameIconViewTemplate];
}
- (void)statusBarItemClicked:(NSStatusItem *)sender
{
NSApplication *app = [NSApplication sharedApplication];
if (app.isHidden) {
[app unhide:sender];
}
if (app.isActive == NO) {
// 激活窗口,否则窗口会出现无法响应事件的问题
[app activateIgnoringOtherApps:YES];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateAllWindows];
}
// 建议指定窗口前置
NSWindow *window = [self anyWindow];
[window makeKeyAndOrderFront:nil];
}
- (NSWindow *)anyWindow
{
//return self.mainWindowController.window;
//return [NSApplication sharedApplication].mainWindow;
for (NSWindow *window in [NSApplication sharedApplication].windows) {
if (window != self.statusBarItem.button.window) {
return window;
}
}
return nil;
}
AppDelegate - Status Bar Item.gif
#pragma mark 为程序坞(dock)添加菜单
- (nullable NSMenu *)applicationDockMenu:(NSApplication *)sender
{
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
NSMenuItem *firstItem = [menu addItemWithTitle:@"First Dock Menu Item"
action:@selector(dockMenuItemClicked:)
keyEquivalent:@""];
firstItem.enabled = YES;
firstItem.target = self;
NSMenuItem *secondItem = [menu addItemWithTitle:@"Second Dock Menu Item"
action:@selector(dockMenuItemClicked:)
keyEquivalent:@""];
secondItem.enabled = YES;
secondItem.target = self;
return menu;
}
- (void)dockMenuItemClicked:(NSMenuItem *)sender
{
NSLog(@"%@ Clicked",sender.title);
}
#pragma mark 程序坞(dock)上的图标被点击了
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
{
if (flag) {
return YES;
}
// 建议指定窗口前置
[[self anyWindow] makeKeyAndOrderFront:nil];
return YES;
}
AppDelegate - Dock Menu.png
#pragma mark 应用程序将要退出,一般在提示重要数据未保存时使用
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
/* 需要在info.plist里加上这个,否则只会执行一次或不执行
<key>NSSupportsSuddenTermination</key>
<false/>
*/
NSAlert *alert = [NSAlert new];
alert.messageText = @"Application should terminate";
alert.alertStyle = NSAlertStyleCritical;
[alert addButtonWithTitle:@"Quit"].tag = NSModalResponseOK;
[alert addButtonWithTitle:@"Cancel"].tag = NSModalResponseCancel;
[alert beginSheetModalForWindow:[self anyWindow] completionHandler:^(NSModalResponse returnCode) {
BOOL quit = returnCode == NSModalResponseOK;
[[NSApplication sharedApplication] replyToApplicationShouldTerminate:quit];
}];
return NSTerminateLater;
/*
NSModalResponse response = [alert runModal];
return response == NSModalResponseOK ? NSTerminateNow : NSTerminateCancel;
*/
}
#pragma mark 应用程序将要退出,清理缓存和关闭某些功能时用到
- (void)applicationWillTerminate:(NSNotification *)aNotification {
/* 需要在info.plist里加上这个,否则不会执行
<key>NSSupportsSuddenTermination</key>
<false/>
*/
NSLog(@"Application will terminate...");
}
AppDelegate - App Should Terminate.gif
#pragma mark 其他方法
- (void)applicationWillBecomeActive:(NSNotification *)notification
{
// 应用程序将要激活
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
// 应用程序已激活
}
- (void)applicationWillResignActive:(NSNotification *)notification
{
// 应用程序将要挂起
}
- (void)applicationDidResignActive:(NSNotification *)notification
{
// 应用程序已挂起
}
/*
- (void)applicationWillUpdate:(NSNotification *)notification
{
// 应用程序被激活后,任何操作都会触发此事件
}
- (void)applicationDidUpdate:(NSNotification *)notification
{
// 应用程序被激活后,任何操作都会触发此事件
}
- (void)applicationDidHide:(NSNotification *)notification
{
// 应用程序已隐藏
}
- (void)applicationWillUnhide:(NSNotification *)notification
{
// 应用程序将要取消隐藏
}
- (void)applicationDidUnhide:(NSNotification *)notification
{
// 应用程序已取消隐藏
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
// 所有窗口被关闭时退出程序,会触发[AppDelegate applicationShouldTerminate:];
return YES;
}
*/
@end