“Lock” Screen On Mac App

南楼画角 提交于 2019-12-22 08:38:20

问题


In Mac OS X, what API do I need to call in order to place a window over not only the entire screen, but the menu bar and dock as well? Also, is it possible to effectively "lock" the screen into this position, disabling Mission Control, launchpad, etc.?

I have tried the following code within the App Delegate's implementation file:

- (void)awakeFromNib {
    @try {
        NSApplicationPresentationOptions options = NSApplicationPresentationDisableForceQuit + NSApplicationPresentationDisableHideApplication + NSApplicationPresentationDisableProcessSwitching + NSApplicationPresentationHideDock + NSApplicationPresentationHideMenuBar + NSApplicationPresentationFullScreen;
        [NSApp setPresentationOptions:options];
        NSLog(@"Set presentation options"); 

    }
    @catch (NSException *exception) {
        NSLog(@"Error. Invalid options");
    }

}

NSLog reads "Set presentation options", but nothing else happens. Any tips?


回答1:


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.




回答2:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/10660996/lock-screen-on-mac-app

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