Connect to WiFi programmatically in ios

2020-02-26 13:47发布

问题:

My question is about private API in IOS. Is there some API to manage WiFi connection? For example I'm using Apple80211.framework to scan WiFi networks but it's unusable for connecting.

My app isn't for appstore.

回答1:

I ran into this issue last year and after quite a bit of digging I came across a helpful example called SOLStumbler. For this app to work your device must be jailbroken. I modified SOLStumbler and threw up a sample app on github. It uses the IPConfiguration bundle to access Apple80211 framework, which allows you to associate to a network. The readme contains the custom build instructions.

https://github.com/devinshively/wifiAssociate



回答2:

We can programmatically connect wifi networks after iOS 11 public API. You can connect wifi using SSID and password like following.

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 occurred
        print(error?.localizedDescription)
    }
    else {
        //success
    }
}

Don't forget import NetworkExtension.



回答3:

No need for private API any more! With iOS 11, Apple provided a public API you can use to programmatically join a WiFi network without leaving your app.

The class you’ll need to use is called NEHotspotConfiguration.

To use it, you need to enable the Hotspot capability in your App Capabilities (Adding Capabilities). Quick working example :

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

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

This will prompt the user to join the “SSID” WiFi network. It will stay connected to the WiFi until the user leaves the app.

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



回答4:

It's possible in any version of iOS which can load a configuration profile: the app needs to generate a profile with the desired network credentials, and host a web server which provides the profile to Mobile Safari.



标签: api ios5 wifi