iOS Question Way to read Wifi SSID

Turbo3

Active Member
Licensed User
Longtime User
Is there a way to read the Wifi SSID?

I would like to verify the user is connected to the correct network before I try to open a socket.

If not can this be added to iNetwork library?

==============
(As long as I am asking, is there a way to read the device's battery charge level?)
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
Thanks for the reply. Hopefully at some future date it will be possible to read the Wifi SSID. Being able to list the currently available SSIDs and connect to one would also be nice. But just being able to tell the current SSID would be great as a first step.

I have created a new thread for how to read the battery level.
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
With inline Objective C can we now have a routine to read the WiFi SSID?

I found this Objective C code. What needs to be done to use it in B4i? I tried surrounding it with #IF OBJC and #end if but I get compile errors.

Do we just use SSID=NativeMe.RunMethod("currentWifiSSID", Null) to call it with SSID being defined as a String?

B4X:
#import <SystemConfiguration/CaptiveNetwork.h>

+ (NSString *)currentWifiSSID {
    // Does not work on the simulator.
    NSString *ssid = nil;
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        if (info[@"SSID"]) {
            ssid = info[@"SSID"];
        }
    }
    return ssid;
}
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Almost (you need to change the + to - as you want an instance method). This code works:
B4X:
Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private NativeMe As NativeObject
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.RootPanel.LoadLayout("1")
   NavControl.ShowPage(Page1)
   NativeMe = Me
   Dim ssid As String = NativeMe.RunMethod("currentWifiSSID", Null).AsString
   Log(ssid)
End Sub

#If OBJC

#import <SystemConfiguration/CaptiveNetwork.h>

- (NSString *)currentWifiSSID {
  // Does not work on the simulator.
  NSString *ssid = nil;
  NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
  for (NSString *ifnam in ifs) {
  NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
  if (info[@"SSID"]) {
  ssid = info[@"SSID"];
  }
  }
  return ssid;
}
#end if
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
Never mind, fixed it. Initialized ssid to "none" before if test. zip has been updated.

(That works fine but if I turn off WiFi the code crashes and since WiFi is off I can't see the error.
What so you think needs to be added to handle the case of WiFi being off?)

Attached is an updated test program for reading the SSID of the currently active WiFi Network . Press label to refresh. If you have WiFi off it will return "none" for the SSID when you refresh.
 

Attachments

  • SSID.zip
    2.4 KB · Views: 465
Last edited:
Upvote 0
Top