在安装之前确定网络共享是否存在

Mat*_*all 5 cocoa objective-c

我正在开发一种工具,根据用户连接的无线网络自动挂载网络卷.安装音量很容易:

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);
Run Code Online (Sandbox Code Playgroud)

但是,如果没有网络共享volumeURL(例如,如果有人关闭或删除了网络硬盘驱动器),Finder会弹出一条错误消息来解释这一事实.我的目标是不发生这种情况 - 我想尝试安装卷,但如果安装失败则无声地失败.

有没有人有关于如何做到这一点的任何提示?理想情况下,我想在尝试安装之前找到一种检查共享是否存在的方法(以避免不必要的工作).如果那是不可能的,那么告诉Finder不显示其错误消息的某种方式也会起作用.

nal*_*all 6

此答案使用私有框架.正如naixn在评论中指出的那样,这意味着它可以在点发布上实现收支平衡.

没有办法只使用公共API(我可以在几个小时的搜索/反汇编后找到).

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

此外,它不是Finder显示这些消息,而是来自CoreServices的NetAuthApp.正在调用here(netfs_MountURLWithAuthenticationSync)的函数直接从question(FSMountServerVolumeSync)中的函数调用.在这个级别调用它让我们传递kSuppressAllUI旗帜.

成功时,rc为0,mountpoints包含已安装目录的NSStrings列表.

//
// 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];
}
Run Code Online (Sandbox Code Playgroud)