skip to Main Content

I don’t find a way to check if wifi is enabled on iOS.
I am currently creating tracking app and to increase user location we need wifi enabled.
So I want to ask user to enable wifi, but I want to check if it’s currently enabled or not.
Is there a way?

3

Answers


  1. With https://github.com/react-native-netinfo/react-native-netinfo you can check it by:

    NetInfo.fetch().then(state => {
      console.log("Connection type", state.type);
      console.log("Is connected?", state.isConnected);
    });
    
    Login or Signup to reply.
  2. You can use this code to check connection type :-

     NetInfo.fetch().then(state => {
      this.setState({
        connType: state.type
      })
     });
    

    If connection type is wifi then ok if not you can display some popup to enable wifi.

    You can use the following code to open settings, so user can easily enable their wifi

      openWiFi = () => {
        if (Platform.OS === "ios") {
            Linking.openURL('app-settings:')
        }else{
            // import DeviceSettings from 'react-native-device-settings';
            DeviceSettings.wifi();
        }
       }
    
    Login or Signup to reply.
  3. You can get desired status by below method. Write this method in native & access from React Native by using promise.
    Refer:https://francescocrema.it/check-wifi-status-on-ios-in-swift/

    func isWiFiOn() -> Bool {
            var address : String?
            var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
            if getifaddrs(&ifaddr) == 0 {
                var ptr = ifaddr
                while ptr != nil {
                    defer { ptr = ptr.memory.ifa_next }
                    let interface = ptr.memory
                    let addrFamily = interface.ifa_addr.memory.sa_family
                    if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
                        if let name = String.fromCString(interface.ifa_name) where name == "awdl0" {
                            if((Int32(interface.ifa_flags) & IFF_UP) == IFF_UP) {
                                return(true)
                            }
                            else {
                                return(false)
                            }
                        }
                    }
                }
                freeifaddrs(ifaddr)
            }
            return (false)
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search