How to check network provider name in iOS programming?

天涯浪子 提交于 2019-12-06 13:01:03

问题


I need to check whether device has been connected properly to "My-Wifi" network or not. If it is connected then I will send some data to server otherwise not.

Right now I am just checking with the Internet connection, using Reachability class.

So how to check that?


回答1:


You can make use of CNCopySupportedInterfaces() call.

CFArrayRef interfaces = CNCopySupportedInterfaces();
CFIndex count = CFArrayGetCount(interfaces);

for (int i = 0; i < count; i++) {
    CFStringRef interface = CFArrayGetValueAtIndex(interfaces, i);
    CFDictionaryRef netinfo = CNCopyCurrentNetworkInfo(interface);
    if (netinfo && CFDictionaryContainsKey(netinfo, kCNNetworkInfoKeySSID)) {
        NSString *ssid = (__bridge NSString *)CFDictionaryGetValue(netinfo, kCNNetworkInfoKeySSID);
        // Compare with your needed ssid here
    }

    if (netinfo)
        CFRelease(netinfo);
}
CFRelease(interfaces);

In my experience, you will usually have one interface in the array which would either be a valid structure if you're connected or NULL if you're not. Still I let the for loop be there just in case.

The __bridge cast inside is only needed if you're using ARC.



来源:https://stackoverflow.com/questions/9342225/how-to-check-network-provider-name-in-ios-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!