“Lock” Screen On Mac App

好久不见. 提交于 2019-12-05 18:08:38

This would basically involve the same sorts of thing as "kiosk mode". See Apple's Kiosk Mode Programming Topic.

You basically use -[NSApplication setPresentationOptions:] or -[NSView enterFullScreenMode:withOptions:] with an option dictionary containing the key NSFullScreenModeApplicationPresentationOptions whose value is an NSNumber containing the same sort of presentation option values as the NSApplication method takes.

The options are |'d together using a bitwise OR:

NSApplicationPresentationOptions options = NSApplicationPresentationDisableForceQuit | NSApplicationPresentationDisableHideApplication | NSApplicationPresentationDisableProcessSwitching | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar | NSApplicationPresentationFullScreen; [NSApp setPresentationOptions:options];

ma11hew28

In Xcode, create a new Cocoa Application, and paste the code below in AppDelegate.m.

- (void)awakeFromNib
{
    // Lock app in full screen mode for 10 seconds.
    NSApplicationPresentationOptions presentationOptions = (NSApplicationPresentationHideDock |
                                                            NSApplicationPresentationHideMenuBar |
                                                            NSApplicationPresentationDisableAppleMenu |
                                                            NSApplicationPresentationDisableProcessSwitching |
                                                            NSApplicationPresentationDisableForceQuit |
                                                            NSApplicationPresentationDisableSessionTermination |
                                                            NSApplicationPresentationDisableHideApplication);
    NSDictionary *fullScreenOptions = @{NSFullScreenModeApplicationPresentationOptions: @(presentationOptions)};
    [_window.contentView enterFullScreenMode:[NSScreen mainScreen] withOptions:fullScreenOptions];
    [_window.contentView performSelector:@selector(exitFullScreenModeWithOptions:) withObject:nil afterDelay:10.0];
}

You will still be able to quit the app with ⌘Q. To prevent that, you can delete the Key Equivalent of the Quit Menu Item.

Or, you can subclass NSApplication and override -sendEvent: to do nothing, thereby ignoring all events (keyboard, mouse, etc.) sent to your application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!