【问题标题】:Detect volume mount and get its path检测卷挂载并获取其路径
【发布时间】:2015-08-26 09:38:27
【问题描述】:

我需要检测何时在 OS X 中安装卷并获取其完整路径。

我已经成功实现了检测何时安装卷的代码,但是,我仍在努力获取它的路径。

检测何时挂载卷的代码如下所示:

-(void) monitorVolumes
{
    // Notification for Mountingthe USB device

    [[[NSWorkspace sharedWorkspace] notificationCenter]addObserver:self selector: @selector(onVolumeMount:)
      name: NSWorkspaceDidMountNotification object: nil];

    // Notification for Un-Mountingthe USB device

    [[[NSWorkspace sharedWorkspace] notificationCenter]addObserver:self selector: @selector(onVolumeMount:)
      name: NSWorkspaceDidUnmountNotification object: nil];

}

-(void) onVolumeMount: (NSNotification*) notification
{
    NSLog(@"Volume Mount");

   //Code to get path here...
}

-(void) onVolumeUnmount: (NSNotification*) notification
{
    NSLog(@"Volume Unmount");
}

我不知道如何正确获取路径。

如何做到这一点?

【问题讨论】:

    标签: macos volume detect disk


    【解决方案1】:

    在通知的userInfo 字典中,在NSWorkspaceVolumeURLKey 键下,您会找到音量的NSURL。如果你真的需要一个路径字符串,你可以向NSURL 询问它的path

    NSString* path = [notification.userInfo[NSWorkspaceVolumeURLKey] path];
    

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 2016-04-03
      • 2020-09-15
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 2021-04-11
      相关资源
      最近更新 更多