Certain bandwidth-intensive things could benefit from first checking whether the user is on WiFi, or over cellular, because many people (me) have limited data per month. There seems to be an apple library called
reachability, but it needs to be installed on a per app basis, or something like that.
How can I detect wifi vs cellular in Pythonista?
Here's an example of using reachability APIs using
There are certain edge cases that aren't handled by this example (e.g. it could happen that the WiFi connection is lost after you check but before you start the download etc.), but it should be good enough in most cases.
from ctypes import * def is_cellular_connection(): c = cdll.LoadLibrary(None) FLAG_WWAN = (1 << 18) create = c.SCNetworkReachabilityCreateWithName create.restype, create_argtypes = c_void_p, [c_void_p, c_char_p] get_flags = c.SCNetworkReachabilityGetFlags get_flags.restype, get_flags.argtypes = c_bool, [c_void_p, POINTER(c_uint32)] r = create(None, 'google.com') flags = c_uint32(0) get_flags(r, byref(flags)) return bool(flags.value & FLAG_WWAN)