如何以编程方式连接到给定 SSID 和密码的 WiFi 网络

How to programmatically connect to a WiFi network given the SSID and password

我正在尝试复制我向 iOS 提出的现有 Android 申请。在我的应用程序中,我应该能够连接到给定 SSID 和密码的 WiFi 网络。 SSID 和密码可以是硬编码的,也可以由用户输入。我正在通过互联网研究如何在 iOS 上完成此操作,但是,似乎非常不鼓励这种行为,并且没有办法使用无法获得的 public/documented 库来执行此操作你的申请被 App Store 拒绝了。

问题是我将亲自使用这个应用程序,我不打算将它提交到 App Store,所以如果我使用外部库也没关系。如果我的朋友想要使用它,我可以导出一个企业 ipa 并给他们安装说明。

经过搜索,MobileWifi.framework似乎是一个不错的候选人。但是,在给定 SSID 和密码的情况下,似乎没有使用此库连接到 WiFi 网络的直接方法。

有没有人根据 SSID 和密码成功尝试连接到 Wifi 网络?

简答,不。


长答案:)

这个问题被问了很多次:

  • 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

最有趣的答案似乎在第一个 link 中,它指向一个 GitHub 项目:wifiAssociate。然而,第三个 link 中的某些人解释说,这不再适用于 iOS8,因此您可能很难获得它 运行。
此外,iDevice 必须已越狱。

与您在这里和其他地方看到的相反,您可以做到。很难让普通用户足够漂亮,但如果不是必须那么编码真的很容易。这是企业管理员的事情,但任何人都可以做到。例如,查看 "Connection Profiles." Comcast 在他们的 iOS 应用程序中为您设置热点。

基本上,它是一个 XML 文档,您可以让设备通过 Safari 或 Mail 获取该文档。它有很多技巧并且用户体验不是很好,但我可以确认它在 iOS 10 中工作并且不需要越狱。您使用 Configurator 生成 XML,我建议 this thread for serving it (but people don't find it because it's not specifically about WiFi), and this blog 查询它是否已安装,这非常重要但确实有效。

I've answered this question 很多次,但要么没有足够的代表,要么太笨不知道如何将问题作为重复问题关闭,以便更容易找到更好的答案。

UPDATE: iOS 11 直接为此功能提供 API(终于!)。参见 NEHotspotConfiguration。我们的 Xamarin C# 代码现在看起来像:

var config = new NEHotspotConfiguration(Ssid, Pw, false);
config.JoinOnce = false;

使用 iOS 11,Apple 提供 public API 您可以使用它以编程方式加入 WiFi 网络,而无需离开您的应用程序。

您需要使用的 class 称为 NEHotspotConfiguration

要使用它,您需要在您的应用功能 (Adding Capabilities) 中启用 Hotspot 功能。快速工作示例:

NEHotspotConfiguration *configuration = [[NEHotspotConfiguration
 alloc] initWithSSID:@“SSID-Name”];
configuration.joinOnce = YES;

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:nil];

这将提示用户加入“SSID-Name”WiFi 网络。它将保持连接到 WiFi,直到用户离开应用程序。

这不适用于模拟器,您需要 运行 将此代码与实际设备配合使用才能使其正常工作。

更多信息在这里: https://developer.apple.com/documentation/networkextension/nehotspotconfiguration

我们可以在 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
            }
        }

您可以使用 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)
    }

我的两分钱: 如果你有:

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

只是它在模拟器下不起作用。

在真实设备上它确实可以编译。 所以使用:

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
    }

在iOS11中连接wifi网络。您可以使用如下的ssid和密码连接wifi。

在应用程序 ID 配置服务上启用热点

启用热点配置后

Swift 4.0 代码仅适用于 iOS 11:

import NetworkExtension

...

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")
    }
}

确保功能中的网络扩展和热点配置都已打开。

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
            }
    }

我不确定这是否对任何人有帮助,但如果我没记错的话,Apple 从第 9 版开始就在 iOS 中加入了一个 Wifi 二维码。已经有一段时间了。

你可以去这里:https://qifi.org

输入您的 SSID 和密码。打印二维码或将其保留在屏幕上。使用相机应用程序将 iPhone 指向二维码。您将看到一个通知栏,如果您按下,将即时设置您的 WIFI 连接。

此 'should' 可与 Android 一起使用,但我尚未对其进行测试。它确实适用于 iOS。

这个解决方案的好处是你可以让你的 WIFI 基地 'hidden' 在网络上,人们仍然可以连接到它,出于安全原因,这在高流量区域是非常可取的。