network.setStatusListener()

Type Function
Library network.*
Return value none
Revision 2017.3060
Keywords network status, reachability, setStatusListener
See also network.canDetectNetworkStatusChanges
networkStatus

Overview

Starts monitoring a host for its network reachability status. This API is designed around the idea that you are monitoring individual hosts and not the hardware directly. Potentially, some hosts might continue to be reachable while others are not due to internet conditions, firewall configurations, authentication rules, and whether you need full DNS routing.

Gotchas

Syntax

network.setStatusListener( hostURL, listener )
hostURL (required)

String. The host you want to monitor. This may be something like "www.coronalabs.com". You should not include http:// or the port number.

listener (required)

Listener. The listener that gets networkStatus events for the specified host. Pass nil to unregister the listener that's set for the specified host.

Example

function networkListener( event )
    print( "address", event.address )
    print( "isReachable", event.isReachable )
    print( "isConnectionRequired", event.isConnectionRequired )
    print( "isConnectionOnDemand", event.isConnectionOnDemand )
    print( "IsInteractionRequired", event.isInteractionRequired )
    print( "IsReachableViaCellular", event.isReachableViaCellular )
    print( "IsReachableViaWiFi", event.isReachableViaWiFi )
    -- If you want to remove the listener, call network.setStatusListener( "www.apple.com", nil ) 
end
 
if ( network.canDetectNetworkStatusChanges ) then
    network.setStatusListener( "www.apple.com", networkListener )
else
    print( "Network reachability not supported on this platform." )
end