Internet Connection?


#1

How i do to ask if there if any internet connection in my device ?


#2

I don’t think OpenFL provides this, so you’ll have to make an extension.

Follow the instructions in that post, but instead of SetBrightness.mm, make a file named Network.mm containing this:

#import "Network.h"

namespace network {
	bool isConnected() {
		return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]
			!= NotReachable;
	}
}

Then add Network.h:

#ifndef NETWORK_H
#define NETWORK_H

#import "Reachability.h"

namespace network {
	bool isConnected();
}

#endif

And then use this for Network.java:

package com.your.package;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import org.haxe.extension.Extension;

public class Network {
	private static NetworkInfo getActiveNetworkInfo() {
		ConnectivityManager cm =
			(ConnectivityManager)Extension.mainContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		return cm.getActiveNetworkInfo();
	}
	
	public static boolean isConnected() {
		NetworkInfo activeNetwork = getActiveNetworkInfo();
		return activeNetwork != null && activeNetwork.isConnected();
	}
}

For iOS, you’ll also need to include Apple’s Reachability class. Go here, click “Download Sample Code,” and then extract Reachability.h and Reachability.m.


#3

Happy for pull requests, or to help extension code that we think should be in the core :slight_smile: