简体   繁体   English

React-native 询问 iOS 启用 wifi

[英]React-native ask iOS to enable wifi

I don't find a way to check if wifi is enabled on iOS.我找不到检查 iOS 上是否启用 wifi 的方法。 I am currently creating tracking app and to increase user location we need wifi enabled.我目前正在创建跟踪应用程序并增加我们需要启用 wifi 的用户位置。 So I want to ask user to enable wifi, but I want to check if it's currently enabled or not.所以我想要求用户启用wifi,但我想检查它当前是否启用。 Is there a way?有办法吗?

With https://github.com/react-native-netinfo/react-native-netinfo you can check it by:使用https://github.com/react-native-netinfo/react-native-netinfo ,您可以通过以下方式进行检查:

NetInfo.fetch().then(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
});

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.如果连接类型是 wifi,那么可以,如果不是,您可以显示一些弹出窗口来启用 wifi。

You can use the following code to open settings, so user can easily enable their wifi您可以使用以下代码打开设置,以便用户轻松启用他们的 wifi

  openWiFi = () => {
    if (Platform.OS === "ios") {
        Linking.openURL('app-settings:')
    }else{
        // import DeviceSettings from 'react-native-device-settings';
        DeviceSettings.wifi();
    }
   }

You can get desired status by below method.您可以通过以下方法获得所需的状态。 Write this method in native & access from React Native by using promise.使用 promise 在本机中编写此方法并从 React Native 访问。 Refer: https://francescocrema.it/check-wifi-status-on-ios-in-swift/参考: 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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM