3

I'm developing an app in which I need to know:

  1. Does my app has the permission to access WLAN network.
  2. Is the device currently connected to a WIFI.

I know you can use Reachability to check if the device is connected to the internet, but it return true even if the app doesn't has the permission to access network. How can I know the 2 information I listed above ?

Additional information: This is not a regular app. This app is used to control a toy via WIFI. So I don't really care if the app is connected to the internet. All I want to know is if the app has the permission to access WLAN network.

guocongyu
  • 133
  • 1
  • 8

1 Answers1

6

If you are worried that there may be a captive portal giving a false "connected" status to Reachability then you need to attempt to retrieve a known piece of data from a known website; then if you get the expected result you can be fairly confident that your app has network access.

For example, http://captive.apple.com returns:

<HTML>
  <HEAD>
   <TITLE>Success</TITLE>
  </HEAD>
  <BODY>
     Success
  </BODY>
</HTML>

This is the web site that iOS and macOS attempt to access in order to determine whether the hotspot login helper window should be shown.

If iOS doesn't get the expected "success" (as is the case if all access is redirected to a "login" screen) or the connection times out then the helper window is shown.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • But sometimes if we connected to network that has firewall. At that network your will be connected but can't access it because you must do login in network so how can we recognise that we logged in OR not ? – Govaadiyo Sep 29 '17 at 06:12
  • 1
    That is exactly what I described in my answer; you connect to a known site and retrieve some known data; if that connection works and you get back the known data you are good to go. If the connection fails or you get back some other data (say a "login" screen) then there is a problem. – Paulw11 Sep 29 '17 at 06:14
  • Thanks for the reply, sorry I missed some key information in my question description. I've added additional information. – guocongyu Sep 29 '17 at 10:23
  • If all you need is local network access then there is no way you can't have "permission". Either you have a WiFi connection or you don't. If the WiFi network requires a passkey and the user hasn't; entered it then you won't be connected. If the network doesn't require a passkey or the user has entered a passkey then it doesn't matter if there is some router preventing you from accessing the Internet, you will have access to the local network. The issue you will have is that Reachability uses the ability to contact Internet hosts to determine network availability – Paulw11 Sep 29 '17 at 10:43
  • The code in [this answer](https://stackoverflow.com/questions/39558868/check-internet-connection-ios-10) that Tofaani linked to earlier may help you; it returns true if you are connected to WiFi even if there is no Internet on that WiFi. – Paulw11 Sep 29 '17 at 10:48
  • I guess I made a mistake when I did Reachability experiment earlier today, Thanks for your reply. – guocongyu Sep 29 '17 at 12:02