2009-10-01 80 views
5

我正在研究一种基于用户所连接的无线网络自动安装网络卷的工具。安装的体积很容易:确定在安装之前网络共享是否存在

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不要显示其错误信息的方法也可以。

回答

6

此答案使用私人框架。正如naixn在评论中指出的那样,这意味着它可能会在点击发布时达到平衡。

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

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

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

成功时,rc为0,挂载点包含挂载目录的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]; 
} 
+1

请注意,kSoftMount && kSuppressAllUI以及netfs_MountURLWithAuthenticationSync都来自PrivateFrameworks。 这意味着它可能适用于您当前的系统,但可能在将来(甚至点)版本中被打破。 – 2009-10-02 17:51:56

+0

绝对正确。我只发布这个,因为没有办法用当前的公共API来做到这一点。 – nall 2009-10-02 18:01:08

+0

大家好, 我被困在同样的问题了! 我们有一个更清洁的解决方案可用于解决这个问题吗? 我试过使用这个API,但我得到错误,“netfs_MountURLWithAuthenticationSync”是一个未知的标识符... – PRIME 2015-04-15 09:16:09

相关问题