如何检测OSX管理员密码提示?

Joh*_*rak 5 macos objective-c nsnotificationcenter

我正在尝试(以编程方式)检测更改系统安全设置时出现的OSX管理员密码提示.理想情况下,解决方案适用于C++或Objective-C.我查看了各种NSDistributedNotificationCenters提供操作系统通知的内容,但它们似乎都不是特定于密码提示的.我已经尝试注册操作系统可以提供的所有通知,但是一旦我进入"系统偏好设置"窗口,这些通知似乎就会停止.

我也研究了这个SFAuthorizationPlugin概念,但似乎更多的是从冷启动登录系统.

我知道这是可能的,因为我已经看到其他应用程序检测到密码提示并在屏幕上显示某些内容.

那么如何以编程方式检测OSX管理员密码提示?

Che*_*lor 4

您可以从工作区监听 SecurityAgent 通知。

订阅应用程序激活通知,如下所示:

@interface notificationHandler: NSObject {}
@end

@implementation notificationHandler
-(id)init
{
    [[[NSWorkspace sharedWorkspace] notificationCenter]
        addObserver:self
        selector   :@selector(handleNotification)
        name       :NSWorkspaceDidActivateApplicationNotification
        object     :nil];
} // init

-(void)handleNotification:(NSNotification *) notification
{
    NSDictionary info = [notification userInfo];
    NSString *appName = [[info objectForKey:NSWorkspaceApplicationKey] localizedName];
    if ([appName isEqualToString:@"SecurityAgent"]) {
        // You have found the administrator password prompt!
    }
} // handleNotification
@end
Run Code Online (Sandbox Code Playgroud)