簡體   English   中英

在 FSEvent 回調中訪問 app delegate function

[英]Access app delegate function in FSEvent callback

我在應用程序委托文件中創建了這個 function。 我想在回調中調用 function 的應用程序委托。

請問有什么辦法嗎。

-(void) monitor{
    FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                  &feCallback,
                                                  &cntxt, 
                                                  pathsToWatch, 
                                                  kFSEventStreamEventIdSinceNow, 
                                                  1,
                                                  kFSEventStreamCreateFlagWatchRoot );
} 

static void feCallback(ConstFSEventStreamRef streamRef,
                       void* pClientCallBackInfo,
                       size_t numEvents,
                       void* pEventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[]) 
{
    NSLog(@"The file changed!"); 
    // need to to call app delegate function
}

由於 FSEventStreamCreate 僅在 macOS 上可用,您可以通過以下方式進行:

    // call this in init of your app delegate
    FSEventStreamContext  cntxt = {0, (__bridge void *)(self), NULL, NULL, NULL};
    // call FSEventStreamCreate as in your code
    // keep a reference to the stream so you can stop and start it later

然后在您的回調中,AppDelegate 將位於:

    YourAppDelegateClass* appDele = (__bridge YourAppDelegateClass*)pClientCallBackInfo; 

FSEvents 的整個機制非常復雜,我們必須在正確的運行循環中安排它並使用 ARC 和類型轉換。 回調在 ObjectiveC 之外,您將無法使用 Cocoa,而只能使用 CFString 和其他 Core Foundation 類型。 不僅閱讀文檔而且查看框架中的 FSEvents.h 也很有幫助(任何函數的上下文菜單,然后跳轉到定義:要閱讀的內容比文檔中的內容更多)。 根據您的發行版,還可以使用 App Store 沙盒和安全范圍內的書簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM