I have the following code to detect the connection type. Android works fine but for iOS it returns unknown
NetInfo.getConnectionInfo().then((connectionInfo) => {
console.tron.log('connectionInfo')
console.tron.log(connectionInfo)
})
I am currently using RN version : 0.50.4 and iOS 11.3
Thanks for posting this! It looks like your issue may be incomplete. Are all the fields required by the Issue Template filled out?
If you believe your issue contains all the relevant information, let us know in order to have a maintainer remove the No Template label. Thank you for your contributions.
Hi, here is a workaround if you want to make it work for both Android/iOS
const getConnectionInfo = async () => {
if (Platform.OS === 'ios') {
return new Promise((resolve, reject) => {
const connectionHandler = connectionInfo => {
NetInfo.removeEventListener('connectionChange', connectionHandler)
resolve(connectionInfo)
}
NetInfo.addEventListener('connectionChange', connectionHandler)
})
}
return NetInfo.getConnectionInfo()
}
and then when you want to use it instead of directly call
const connectionInfo = await NetInfo.getConnectionInfo()
you do it that way
const connectionInfo = await getConnectionInfo()
From, of course, an asynchrone function other wise it won't work because of the "await" word
Gotcha. Glad it worked.
This issue was marked as lacking information required by the issue template. There has been no activity on this issue for a while, so I will go ahead and close it.
If you found this thread after encountering the same issue in the latest release, please feel free to create a new issue with up-to-date information by clicking here.
If you are the author of this issue and you believe this issue was closed in error (i.e. you have edited your issue to ensure it meets the template requirements), please let us know.
@capozzid Thanks for the workaround!
Most helpful comment
Hi, here is a workaround if you want to make it work for both Android/iOS
and then when you want to use it instead of directly call
const connectionInfo = await NetInfo.getConnectionInfo()you do it that way
const connectionInfo = await getConnectionInfo()From, of course, an asynchrone function other wise it won't work because of the "await" word