2012-06-28 78 views
0

當機場連接到ap時,我需要系統發出通知。 SystemConfiguration框架有沒有可能做到這一點?我有問題了解系統配置api文檔。WiFi連接時的通知(OS X)

感謝您的幫助。

回答

2

您在SystemConfiguration網絡的正確軌道上,它提供SCNetworkReachability功能集。您可以嘗試使用

SCNetworkReachabilitySetCallback 

設置回調,當可達性發生變化時將會調用回調;和

SCNetworkReachabilityScheduleWithRunLoop 

調度運行循環

內的可達性檢查或者你可以嘗試使用reachability framework(無論是MacOS和iOS版),這是建立在SystemConfiguration框架之上,使事情變得更簡單(更高層次)。

如果你想要去的SystemConfiguration方式,這是你如何檢查當前可達性和安裝的回調通知的變化(source):

- (void)checkReachability { 
    NSString *server = [[NSUserDefaults standardUserDefaults] stringForKey:@"NCIDServer"]; 

    if (server == nil) { 
    ncid_message_callback(self, [NSLocalizedString(@"No caller ID server was specified.", nil) UTF8String]); 
    return; 
    } 

    const char *serverName = [[[server componentsSeparatedByString:@":"] objectAtIndex:0] UTF8String]; 
    SCNetworkReachabilityContext context = {0, (void *)self, NULL, NULL, NULL}; 
    networkReachability = SCNetworkReachabilityCreateWithName(NULL, serverName); 

    if (networkReachability == NULL) 
    goto fail; 

    // If reachability information is available now, we don't get a callback later 
    SCNetworkConnectionFlags flags; 

    if (SCNetworkReachabilityGetFlags(networkReachability, &flags)) 
    networkReachabilityCallback(networkReachability, flags, self); 

    if (!SCNetworkReachabilitySetCallback(networkReachability, networkReachabilityCallback, &context)) 
    goto fail; 

    if (!SCNetworkReachabilityScheduleWithRunLoop(networkReachability, [[NSRunLoop currentRunLoop] getCFRunLoop], kCFRunLoopCommonModes)) 
    goto fail; 
    return; 

fail: 
    if (networkReachability != NULL) 
    CFRelease(networkReachability); 

    networkReachability = NULL; //-- ivar representing current reachability 

} 

這是回調的示例:

static void networkReachabilityCallback(SCNetworkReachabilityRef target, 
       SCNetworkConnectionFlags flags, 
       void *object) { 
    // Observed flags: 
    // - nearly gone: kSCNetworkFlagsReachable alone (ignored) 
    // - gone: kSCNetworkFlagsTransientConnection | kSCNetworkFlagsReachable | kSCNetworkFlagsConnectionRequired 
    // - connected: kSCNetworkFlagsIsDirect | kSCNetworkFlagsReachable 

    if (networkReachability == NULL) 
    return; 

    if ((flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired)) { 

    if (isReachable) // typically receive a reachable message ~20ms before the unreachable one 
    return; 

    isReachable = YES; 
    ncid_network_kill(); 
    [NSThread detachNewThreadSelector:@selector(runThread:) toTarget:object withObject:nil]; 

    } else { 
    isReachable = NO; 
    ncid_network_kill(); 
    } 
} 
+0

我使用的可達性框架,它爲我工作好。謝謝。 – dnlkng

0
+0

需要注意的是,OP詢問的是OSX,而​​不是iOS,而Apple的發佈線程和Reachability類則針對iOS。 – sergio

+0

@sergio - 錯過了,會刪除。 – rishi