如何在iOS编程中检查网络提供商名称? [英] How to check network provider name in iOS programming?

查看:168
本文介绍了如何在iOS编程中检查网络提供商名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查设备是否已正确连接到我的Wifi网络。如果它连接,那么我将发送一些数据到服务器,否则不会。

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.

现在我只是检查与互联网连接,使用Reachability类。

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

那么如何检查呢?

推荐答案

调用 CNCopySupportedInterfaces()

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);

在我的经验中,你通常在数组中有一个接口,您已连接或 NULL (如果您不是)。

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.

__ bridge 里面的内容只有在你需要的时候才需要。请使用ARC。

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

这篇关于如何在iOS编程中检查网络提供商名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆