omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Reachability

    Pythonista
    2
    2
    2001
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Webmaster4o
      Webmaster4o last edited by

      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?

      1 Reply Last reply Reply Quote 2
      • omz
        omz last edited by omz

        Here's an example of using reachability APIs using ctypes.
        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)
        
        
        1 Reply Last reply Reply Quote 3
        • First post
          Last post
        Powered by NodeBB Forums | Contributors