在Mac App上“锁定”屏幕

时间:2012-05-18 23:33:40

标签: objective-c macos lockscreen

在Mac OS X中,为了不仅在整个屏幕上放置窗口,还要在菜单栏和底座上放置一个窗口,我需要调用哪些API?此外,是否可以有效地“锁定”屏幕到这个位置,禁用任务控制,启动板等?

我在App Delegate的实现文件中尝试了以下代码:

- (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读取“设置演示选项”,但没有其他事情发生。有什么提示吗?

3 个答案:

答案 0 :(得分:5)

这基本上涉及与“自助服务终端模式”相同的事情。请参阅Apple's Kiosk Mode Programming Topic

您基本上使用-[NSApplication setPresentationOptions:]-[NSView enterFullScreenMode:withOptions:]一个包含密钥NSFullScreenModeApplicationPresentationOptions的选项字典,其值为NSNumber,其中包含与{{相同类型的演示文稿选项值1}}方法需要。

答案 1 :(得分:2)

选项是使用按位OR:

组合在一起的

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

答案 2 :(得分:2)

在Xcode中,创建一个新的Cocoa应用程序,并将以下代码粘贴到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];
}

您仍然可以使用⌘Q退出该应用。为防止这种情况,您可以删除退出菜单项的等效键。

Xcode Interface Builder: Menu Item

或者,您可以subclass NSApplication and override -sendEvent:无所事事,从而忽略发送到您的应用程序的所有事件(键盘,鼠标等)。