我正在研究一种根据用户连接到的无线网络自动挂载网络卷的工具。安装卷很容易:

NSURL *volumeURL = /* The URL to the network volume */

// Attempt to mount the volume
FSVolumeRefNum volumeRefNum;
OSStatus error = FSMountServerVolumeSync((CFURLRef)volumeURL, NULL, NULL, NULL, &volumeRefNum, 0L);

但是,如果volumeURL上没有网络共享(例如,如果有人关闭或卸下了网络硬盘驱动器),则Finder会弹出一条错误消息,说明这一事实。我的目标是不会发生这种情况-我想尝试挂载该卷,但是如果挂载失败,则将以静默方式失败。

有人对如何执行此操作有任何提示吗?理想情况下,我想找到一种方法在尝试挂载共享之前检查共享是否存在(以避免不必要的工作)。如果无法实现,则可以通过某种方法告诉Finder不显示其错误消息。

最佳答案

此答案使用专用框架。正如naixn在评论中指出的那样,这意味着它在点发行时可能会收支平衡。

无法仅使用公共(public)API来执行此操作(经过几个小时的搜索/拆卸,我才能找到该API)。

此代码将访问URL,并且不会显示任何UI元素通过或失败。这不仅包括错误,还包括身份验证对话框,选择对话框等。

另外,它不是Finder显示这些消息,而是来自CoreServices的NetAuthApp。在此调用的函数(netfs_MountURLWithAuthenticationSync)是直接从问题中的函数(FSMountServerVolumeSync)调用的。在此级别调用它可以让我们传递kSuppressAllUI标志。

成功时,rc为0,并且mountpoints包含已安装目录的NSString列表。

//
// compile with:
//
//  gcc -o test test.m -framework NetFS -framework Foundation
include <inttypes.h>
#include <Foundation/Foundation.h>

// Calls to FSMountServerVolumeSync result in kSoftMount being set
// kSuppressAllUI was found to exist here:
// http://www.opensource.apple.com/source/autofs/autofs-109.8/mount_url/mount_url.c
// its value was found by trial and error
const uint32_t kSoftMount     = 0x10000;
const uint32_t kSuppressAllUI = 0x00100;

int main(int argc, char** argv)
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSURL *volumeURL = [NSURL URLWithString:@"afp://server/path"];
    NSArray* mountpoints = nil;

    const uint32_t flags = kSuppressAllUI | kSoftMount;

    const int rc = netfs_MountURLWithAuthenticationSync((CFURLRef)volumeURL, NULL, NULL,
            NULL, flags, (CFArrayRef)&mountpoints);

    NSLog(@"mountpoints: %@; status = 0x%x", mountpoints, rc);

    [pool release];
}

关于objective-c - 挂载之前确定网络共享是否存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1507006/

10-10 20:35