iPhone在没有私人库的情况下获取SSID
我有一个商业应用程序,它有完全合法的理由来查看它所连接的网络的 SSID:如果它连接到第 3 方硬件设备的 Adhoc 网络,则它需要以不同的方式运行连接到互联网。
我所看到的有关获取 SSID 的所有内容都告诉我,我必须使用 Apple80211,据我所知,它是一个私有库。我还读到,如果我使用私人图书馆,苹果将不会批准该应用程序。
我是否陷入了苹果和困境之间,或者我在这里错过了什么?
I have a commercial app that has a completely legitimate reason to see the SSID of the network it is connected to: If it is connected to a Adhoc network for a 3rd party hardware device it needs to be functioning in a different manner than if it is connected to the internet.
Everything I've seen about getting the SSID tells me I have to use Apple80211, which I understand is a private library. I also read that if I use a private library Apple will not approve the app.
Am I stuck between an Apple and a hard place, or is there something I'm missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 iOS 7 或 8 开始,您可以执行此操作(需要 iOS 12+ 的权利,如下所示):
输出示例:
请注意,模拟器不支持 ifs。在您的设备上进行测试。
iOS 12
您必须启用从功能访问 wifi 信息。
Swift 4.2
As of iOS 7 or 8, you can do this (need Entitlement for iOS 12+ as shown below):
Example output:
Note that no ifs are supported on the simulator. Test on your device.
iOS 12
You must enable access wifi info from capabilities.
Swift 4.2
iOS 10 及更高版本的更新
iOS 10 中不再弃用 CNCopySupportedInterfaces。(API 参考)
您需要导入 SystemConfiguration/CaptiveNetwork.h 并将 SystemConfiguration.framework 添加到目标的链接库(在构建阶段)。
这是 swift 中的代码片段 (RikiRiocma 的答案):(
重要: CNCopySupportedInterfaces 返回 nil模拟器。)
对于 Objective-c,请参阅 Esad 的答案此处和下面
iOS 9 的更新
自 iOS 9 起,强制网络已弃用*。 (来源)
*不再弃用在 iOS 10 中,请参见上文。
建议您使用 NEHotspotHelper (来源)
您需要向 Apple 发送电子邮件 [电子邮件受保护] 并请求权利。 (来源)
示例代码(不是我的代码。请参阅 Pablo A 的回答):
旁注:是的,他们在 iOS 9 中弃用了 CNCopySupportedInterfaces,并在 iOS 10 中颠倒了它们的位置。我与一位 Apple 网络工程师进行了交谈,结果发生了逆转在此之前,有很多人提交了 Radar,并在 Apple 开发者论坛上公开讨论了这个问题。
UPDATE FOR iOS 10 and up
CNCopySupportedInterfaces is no longer deprecated in iOS 10. (API Reference)
You need to import SystemConfiguration/CaptiveNetwork.h and add SystemConfiguration.framework to your target's Linked Libraries (under build phases).
Here is a code snippet in swift (RikiRiocma's Answer):
(Important: CNCopySupportedInterfaces returns nil on simulator.)
For Objective-c, see Esad's answer here and below
UPDATE FOR iOS 9
As of iOS 9 Captive Network is deprecated*. (source)
*No longer deprecated in iOS 10, see above.
It's recommended you use NEHotspotHelper (source)
You will need to email apple at [email protected] and request entitlements. (source)
Sample Code (Not my code. See Pablo A's answer):
Side note: Yup, they deprecated CNCopySupportedInterfaces in iOS 9 and reversed their position in iOS 10. I spoke with an Apple networking engineer and the reversal came after so many people filed Radars and spoke out about the issue on the Apple Developer forums.
这是基于 @elsurudo 代码的清理后的 ARC 版本:
Here's the cleaned up ARC version, based on @elsurudo's code:
这适用于我的设备(而不是模拟器)。确保添加系统配置框架。
This works for me on the device (not simulator). Make sure you add the systemconfiguration framework.
此代码可以很好地获取 SSID。
这就是结果:
}
This code work well in order to get SSID.
And this is the results :
}
如果您运行的是 iOS 12,则需要执行额外的步骤。
我一直在努力让这段代码工作,最后在苹果的网站上找到了这个:
“重要的
要在 iOS 12 及更高版本中使用此功能,请在 Xcode 中为您的应用程序启用“访问 WiFi 信息”功能。当您启用此功能时,Xcode 会自动将访问 WiFi 信息权利添加到您的权利文件和应用程序 ID 中。”
https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo
If you are running iOS 12 you will need to do an extra step.
I've been struggling to make this code work and finally found this on Apple's site:
"Important
To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID."
https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo
请参阅 CaptiveNetwork 中的 CNCopyCurrentNetworkInfo: http://developer.apple.com/库/ios/#documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html。
See CNCopyCurrentNetworkInfo in CaptiveNetwork: http://developer.apple.com/library/ios/#documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html.
这是简短的&甜蜜的斯威夫特版本。
请记住链接并导入框架:
定义方法:
在需要时调用该方法:
正如其他地方提到的,这仅适用于您的 iDevice。当不在 WiFi 上时,该方法将返回 nil – 因此是可选的。
Here's the short & sweet Swift version.
Remember to link and import the Framework:
Define the method:
Call the method when you need it:
As mentioned elsewhere, this only works on your iDevice. When not on WiFi, the method will return nil – hence the optional.
对于 iOS 13
从 iOS 13 开始,您的应用程序还需要访问 Core Location 才能使用
CNCopyCurrentNetworkInfo
功能,除非它配置了当前网络或具有 VPN 配置:这就是您所需要的(请参阅苹果文档):
- 链接
CoreLocation.framework
库- 将
location-services
添加为 Info.plist 中的UIRequiredDeviceCapability
键/值- 在 Info.plist 中添加
NSLocationWhenInUseUsageDescription
键/值,描述为什么您的应用需要 Core Location- 为您的应用添加“访问 WiFi 信息”权利
现在作为 Objective-C 示例,在使用
CNCopyCurrentNetworkInfo
读取网络信息之前首先检查位置访问是否已被接受:For iOS 13
As from iOS 13 your app also needs Core Location access in order to use the
CNCopyCurrentNetworkInfo
function unless it configured the current network or has VPN configurations:So this is what you need (see apple documentation):
- Link the
CoreLocation.framework
library- Add
location-services
as aUIRequiredDeviceCapabilities
Key/Value in Info.plist- Add a
NSLocationWhenInUseUsageDescription
Key/Value in Info.plist describing why your app requires Core Location- Add the "Access WiFi Information" entitlement for your app
Now as an Objective-C example, first check if location access has been accepted before reading the network info using
CNCopyCurrentNetworkInfo
: