断开以太网电缆与计算机的连接或刚刚关闭以太网时出现问题。在这种情况下,某些条目不存在,我的应用程序会崩溃。所以我试图找出如何防止并刚刚找到CFDictionaryContainsKey,但这并不能防止错误。有谁知道也低于 osx 10.6的解决方法?
- (NSString *)checkNetworkInterface
{
SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL);
CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4"));
Boolean ck = CFDictionaryContainsKey( dr, CFSTR("PrimaryInterface"));
NSString *interfaceString;
if (ck) {
CFStringRef interface = CFDictionaryGetValue(dr, CFSTR("PrimaryInterface"));
interfaceString = [NSString stringWithString:( NSString *)interface ];
} else {
interfaceString = [NSString stringWithString:@"" ];
}
CFRelease(dr);
CFRelease(ds);
return interfaceString;
}
如果“状态:/网络/全球/IPv4”不存在,应用程序崩溃:(