How to check network provider name in iOS programming?
Asked Answered
I

1

1

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?

Irremissible answered 18/2, 2012 at 14:53 Comment(1)
Reachability also allowed to check for WiFi as well.Chaldron
D
2

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.

Doubletalk answered 18/2, 2012 at 15:45 Comment(6)
Hey how to use __bridge, yes I am using ARC for my project. What do i do?Irremissible
You use like in the example I provided, when you want to cast from functions that do not return ARC-supported object ownership. For ssid cast, Xcode actually inserted __bridge for me.Doubletalk
What to do i am getting error, "Use of undeclared identifier '__bridge' "Irremissible
Check answer here. You either don't need __bridge or need to turn ARC on.Doubletalk
Yes, I do have ARC Enabled, but getting that error. Even if i remove that __bridge then error changes to "error: cast to 'NSString *' of a non-Objective-C to an Objective-C pointer is disallowed with Automatic Reference Counting"Irremissible
I cannot reproduce such behaviour. It either says "implicit conversion not allowed" if no cast is there at all, or suggests to me inserting __bridge inside the cast when I enter only (NSString *). Maybe someone else can help.Doubletalk

© 2022 - 2024 — McMap. All rights reserved.