Programmatically set wifi hotspot password

1.7k Views Asked by At

I want to set my WiFi hotspot password programmatically for my application so that user don't have to go to the setting menu to check their password.

I am already using NEHotspotNetwork, where it set the password, but here, we need to set the password which is already there in the setting menu for connecting to the network.

It's also helpful if I can get my WiFi hotspot password, from the application without jailbreak my device.

2

There are 2 best solutions below

2
On

You just need to use the following code:

WifiConfiguration netConfig = new WifiConfiguration();
netConfig .preSharedKey = "yourpassword";
0
On

Using the NEHotspotNetwork function register, you can set the password:

NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
                
                if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
                    //Get all available hotspots
                    var list: [NEHotspotNetwork] = cmd.networkList!
                    //Figure out the hotspot you wish to connect to
                    // let desiredNetwork : NEHotspotNetwork? = getBestScanResult(list)
                    
                    var hotspot = [NEHotspotNetwork]()
                    
                    for network in cmd.networkList!
                    {//check for your network ssid and set password
                          network.setConfidence(.high)
                                    network.setPassword("yourpassword") //Set the WIFI password
                                    
                              
                                hotspot.append(network)
                           
                    }
                    
                    
                    let response = cmd.createResponse(NEHotspotHelperResult.success)
                    response.setNetworkList(hotspot)
                    response.deliver() } else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
                    if let network = cmd.network {
                  
     let response = cmd.createResponse(NEHotspotHelperResult.success)
                        response.setNetwork(network)
                        response.deliver() //Respond back }
                } else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
                    //Perform custom authentication and respond back with success
                    // if all is OK
                    let response = cmd.createResponse(NEHotspotHelperResult.success)
                    response.deliver() //Respond back
                }

Also you can use network configuration profile with the help of Apple Configurator 2 tool for your known network.

There you need to setup your WiFi and then after installing the NCP on your device, it will automatically connect with the mentioned network.

But you have to host that file on server cause we can't download profile locally and using local server like GCDServer (tried already).