Swift:如何检查我的应用是否具有WLAN权限并且设备是否已连接到WIFI源? [英] Swift: How to check if my app has WLAN permission and the device is connected to a WIFI source?

查看:171
本文介绍了Swift:如何检查我的应用是否具有WLAN权限并且设备是否已连接到WIFI源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个我需要知道的应用:

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


  1. 我的应用是否有权访问WLAN网络。

  2. 设备当前是否已连接到WIFI。

我知道您可以使用Reachability进行检查如果设备已连接到互联网,但即使应用程序没有访问网络的权限,它也会返回true。我如何知道上面列出的2个信息?

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 ?

其他信息:这不是常规应用。此应用程序用于通过WIFI控制玩具。所以我真的不在乎应用是否连接到互联网。我想知道的是,该应用是否有权访问WLAN网络。

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.

推荐答案

如果您担心可能存在一个强制门户网站给Reachability提供虚假的连接状态,然后您需要尝试从已知网站检索已知的数据;然后,如果你得到了预期的结果,你可以相当确信你的应用有网络访问权限。

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.

例如, http://captive.apple.com 返回:

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

这是iOS和macOS尝试访问的网站,以确定热点是否登录帮助程序窗口应该显示。

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.

如果iOS未获得预期的成功(如果所有访问都被重定向到登录屏幕)或连接超时则显示帮助窗口。

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.

这篇关于Swift:如何检查我的应用是否具有WLAN权限并且设备是否已连接到WIFI源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆