iOS - 如何在给定 SSID 和密码的情况下以编程方式连接到 WiFi 网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36303123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - How to programmatically connect to a WiFi network given the SSID and password
提问by Razgriz
I am trying to replicate an existing Android Application that I made to iOS. In my application, I should be able to connect to a WiFi network given the SSID and the Password. The SSID and the Password can be hardcoded or can be inputted by the user. I was going through the internet to research on how this can be done on the iOS, however, it seems that this behavior is highly discouraged and that there's no way on doing this using public/documented libraries that won't get your application rejected by the App Store.
我正在尝试复制我为 iOS 制作的现有 Android 应用程序。在我的应用程序中,我应该能够连接到给定 SSID 和密码的 WiFi 网络。SSID 和密码可以是硬编码的,也可以由用户输入。我正在通过互联网研究如何在 iOS 上完成此操作,但是,似乎非常不鼓励这种行为,并且无法使用不会让您的应用程序被拒绝的公共/文档库来执行此操作应用商店。
The thing is I'll be using this application personally and I don't plan on submitting it to the App Store so it's okay if I use external libraries. If my friends would want to use it, I could export an enterprise ipa and give them instructions on how to install it.
问题是我将亲自使用这个应用程序,我不打算将它提交到 App Store,所以如果我使用外部库也没关系。如果我的朋友想使用它,我可以导出一个企业 ipa 并指导他们如何安装它。
Upon searching, it seemed that MobileWifi.frameworkwas a good candidate. However, it does not seem that there's a straightforward way of using this library to connect to a WiFi network given the SSID and the Password.
经过搜索,MobileWifi.framework似乎是一个不错的选择。但是,鉴于 SSID 和密码,似乎没有使用此库连接到 WiFi 网络的直接方法。
Is there anyone who has successfully tried to connect to a Wifi Network given the SSID and Password?
有没有人根据 SSID 和密码成功尝试连接到 Wifi 网络?
回答by Enthouan
With iOS 11, Apple provided public API you can use to programmatically join a WiFi network without leaving your app.
在 iOS 11 中,Apple 提供了公共 API,您可以使用它以编程方式加入 WiFi 网络,而无需离开您的应用程序。
The class you'll need to use is called NEHotspotConfiguration
.
您需要使用的类称为NEHotspotConfiguration
。
To use it, you need to enable the Hotspot
capability in your App Capabilities (Adding Capabilities). Quick working example :
要使用它,您需要Hotspot
在 App Capabilities(添加功能)中启用该功能。快速工作示例:
NEHotspotConfiguration *configuration = [[NEHotspotConfiguration
alloc] initWithSSID:@“SSID-Name”];
configuration.joinOnce = YES;
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:nil];
This will prompt the user to join the “SSID-Name” WiFi network. It will stay connected to the WiFi until the user leaves the app.
这将提示用户加入“SSID-Name”WiFi 网络。它将保持与 WiFi 的连接,直到用户离开应用程序。
This doesn't work with the simulator you need to run this code with an actual device to make it work.
这不适用于模拟器,您需要使用实际设备运行此代码才能使其工作。
More informations here : https://developer.apple.com/documentation/networkextension/nehotspotconfiguration
更多信息在这里:https: //developer.apple.com/documentation/networkextension/nehotspotconfiguration
回答by Eliot Gillum
Contrary to what you see here and other places, you can do it. It's hard to make pretty enough for normal users, but if doesn't have to be then it's really easy to code. It's an enterprise admin thing, but anyone can do it. Look up "Connection Profiles." Comcast does this in their iOS app to setup their hotspots for you, for example.
与您在这里和其他地方看到的相反,您可以做到。对于普通用户来说,很难做得足够漂亮,但如果不是必须的话,那么编码真的很容易。这是企业管理方面的事情,但任何人都可以做到。查找“连接配置文件”。例如,康卡斯特在他们的 iOS 应用程序中为您设置热点。
Basically, it's an XML document that you get the device to ingest via Safari or Mail. There's a lot of tricks to it and the user experience isn't great, but I can confirm that it works in iOS 10 and it doesn't require jailbreaking. You use Configurator to generate the XML, and I suggest this threadfor serving it (but people don't find it because it's not specifically about WiFi), and this blogfor querying if it's installed which is surprisingly nontrivial but does indeed work.
基本上,它是一个 XML 文档,您可以通过 Safari 或 Mail 获取设备。它有很多技巧,用户体验也不是很好,但我可以确认它可以在 iOS 10 中运行,并且不需要越狱。您使用 Configurator 生成 XML,我建议使用此线程为其提供服务(但人们没有找到它,因为它不是专门针对 WiFi 的),以及用于查询它是否已安装的博客,这非常重要但确实有效。
I've answered this questionmany times, but either don't have enough rep or am too dumb to figure out how to close questions as duplicate so better answers can be found more easily.
我已经多次回答过这个问题,但要么没有足够的代表,要么太笨,无法弄清楚如何关闭重复的问题,以便更容易找到更好的答案。
UPDATE: iOS 11 provides APIs for this functionality directly (finally!). See NEHotspotConfiguration. Our Xamarin C# code now looks like:
更新:iOS 11 直接为此功能提供了 API(终于!)。请参阅NEHotspotConfiguration。我们的 Xamarin C# 代码现在看起来像:
var config = new NEHotspotConfiguration(Ssid, Pw, false);
config.JoinOnce = false;
回答by Viraj Patel
connect wifi networks in iOS 11. You can connect wifi using ssid and password like following.
在 iOS 11 中连接 wifi 网络。您可以使用 ssid 和密码连接 wifi,如下所示。
Enable Hotspot on App Id configure services
在 App Id 配置服务上启用热点
After Enable Hotspot Configuration
启用热点配置后
Swift 4.0 Code for iOS 11 Only:
仅适用于 iOS 11 的 Swift 4.0 代码:
let configuration = NEHotspotConfiguration.init(ssid: "SSIDname", passphrase: "Password", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
if error != nil {
if error?.localizedDescription == "already associated."
{
print("Connected")
}
else{
print("No Connected")
}
}
else {
print("Connected")
}
}
回答by filaton
Short answer, no.
简短的回答,没有。
Long answer :)
长答案:)
This question was asked many times:
这个问题被问了很多次:
- Connect to WiFi programmatically in ios
- connect to a specific wifi programmatically in ios with a given SSID and Password
- Where do I find iOS Obj-C code to scan and connect to wifi (private API)
- Programmatically auto-connect to WiFi iOS
- 在 ios 中以编程方式连接到 WiFi
- 使用给定的 SSID 和密码在 ios 中以编程方式连接到特定的 wifi
- 我在哪里可以找到 iOS Obj-C 代码来扫描并连接到 wifi(私有 API)
- 以编程方式自动连接到 WiFi iOS
The most interesting answer seems to be in the first link which points to a GitHub project: wifiAssociate. However someones in the third link explains that this doesn't work anymore with iOS8 so you might have hard time getting it running.
Furthermore the iDevice must be jailbroken.
最有趣的答案似乎在指向 GitHub 项目的第一个链接中:wifiAssociate。然而,第三个链接中的某人解释说,这不再适用于 iOS8,因此您可能很难让它运行。
此外,iDevice 必须越狱。
回答by Anders Chaplin
Make sure both Network Extensions & Hotspot Configuration are turned on in Capabilities.
确保在功能中打开了网络扩展和热点配置。
let wiFiConfig = NEHotspotConfiguration(ssid: YourSSID,
passphrase: YourPassword, isWEP: false)
wiFiConfig.joinOnce = false /*set to 'true' if you only want to join
the network while the user is within the
app, then have it disconnect when user
leaves your app*/
NEHotspotConfigurationManager.shared.apply(wiFiConfig) { error in
if error != nil {
//an error occurred
print(error?.localizedDescription)
}
else {
//success
}
}
回答by ingconti
my two cents: if you got:
我的两分钱:如果你有:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_NEHotspotConfigurationManager", referenced from: objc-class-ref in WiFiViewController.o "_OBJC_CLASS_$_NEHotspotConfiguration", referenced from: objc-class-ref in WiFiViewController.o ld: symbol(s) not found for architecture i386
架构 i386 的未定义符号:
“_OBJC_CLASS_$_NEHotspotConfigurationManager”,引用自:WiFiViewController.o 中的 objc-class-ref “_OBJC_CLASS_$_NEHotspotConfiguration”,引用自:WiFiViewController.o 中的 objc-class-ref ld:未找到符号用于架构 i386
simply it does NOT work under simulator.
只是它在模拟器下不起作用。
on real device it does compile. so use:
在真实设备上它确实可以编译。所以使用:
class func startHotspotHelperStuff(){
if TARGET_IPHONE_SIMULATOR == 0 {
if #available(iOS 11.0, *) {
let configuration = NEHotspotConfiguration(ssid: "ss")
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration, completionHandler: { (err: Error?) in
print(err)
})
} else {
// Fallback on earlier versions
}
}// if TARGET_IPHONE_SIMULATOR == 0
}
回答by Savas Adar
We can programmatically connect wifi networks after IOS 11. You can connect wifi using ssid and password like following.
我们可以在 IOS 11 之后以编程方式连接 wifi 网络。您可以使用 ssid 和密码连接 wifi,如下所示。
Swift
迅速
var configuration = NEHotspotConfiguration.init(ssid: "wifi name", passphrase: "wifi password", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
if error != nil {
//an error accured
print(error?.localizedDescription)
}
else {
//success
}
}
回答by Mayur Rathod
You can connect to wifi using Xcode 9 and swift 4
您可以使用 Xcode 9 和 swift 4 连接到 wifi
let WiFiConfig = NEHotspotConfiguration(ssid: "Mayur1",
passphrase: "123456789",
isWEP: false)
WiFiConfig.joinOnce = false
NEHotspotConfigurationManager.shared.apply(WiFiConfig) { error in
// Handle error or success
print(error?.localizedDescription)
}