2009-10-01 61 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

相關問題