Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Wi-fi Connection Status
-
Is there a way for a script to determine whether the iOS device is currently connected to any wi-fi network? I did not find anything on this in the Python documentation.
Thanks.
Jim
-
On my Verizon iPhone 4S (CDMA), when WiFi is off or unavailable (but cellular connectivity might be available), the following will trigger a socket.gaierror error (which you could trap with a try: block):
<pre>import socket
wifi_ip = socket.gethostbyname(socket.gethostname())</pre>Not sure about other models of iOS device, but on mine you don't have a resolvable IP address until you attempt to make a connection - so just querying your IP address information (which doesn't make a connection) fails. If you have a WiFi connection, you have a dedicated, always query-able WiFi IP address and the above code succeeds / doesn't return an error.
If the above code returns an error, then you can run the following to determine if you're connected to the internet (with 3G service but no WiFi):
<pre>s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("google.com",80))
3g_ip = s.getsockname()[0]
s.close()</pre>This bit of code works, where the first failed, because it attempts to connect to the internet first - and then find out what IP address it used to do it afterwards.
Without cellular data or wifi, both of these code snippets fail.
Combined together, the code would look something like:
<pre>import socket
def network_state():
# return values:
# 2 = WiFi is available
# 1 = No WiFi, but cellular is available
# 0 = No network availability at all
net_state = 0
try:
wifi_ip = socket.gethostbyname(socket.gethostname())
# We have an IP, without a connection - likely WiFi
net_state = 2
return net_state
except (OSError, Exception) as e:
net_state = 0
try:
test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_socket.connect(('google.com', 80))
cellular_ip = test_socket.getsockname()[0]
# We have an IP, only after a connection - likely cellular
net_state = 1
return net_state
except (OSError, Exception) as e:
net_state = 0
# We don't appear to have any connection
net_state = 0
return net_state</pre>I make no promises on the reliability of this code :)
As for Pythonista providing this kind of information directly via a custom class, omz would need to use some of this code: https://github.com/tonymillion/Reachability - which is a port of sample code that Apple wrote originally for iOS 4 to iOS 5+ compatible (with support for ARC/garbage collection).
This bit of code would allow Pythonista to directly query the device for what kind of connectivity options are present.
As for which WiFi network you're connected to, the available WiFi networks in the area, etc. - none of that is possible in iOS any more (in either python or ObjC). Apple removed all of the public and private frameworks for that.
That's why you don't see WiFi scanner apps in the app store any more. Apple a.) expressly prohibits them and b.) you literally and technically just can't write that kind of code in iOS any more.
-
Thank you very much for your resoinse. I will give it a try on my AT&T/GSM 4S this weekend.