54

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 how this can be done on 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.

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.

Upon searching, it seemed that MobileWifi.framework was 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.

Is there anyone who has successfully tried to connect to a Wifi Network given the SSID and Password?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Razgriz
  • 7,179
  • 17
  • 78
  • 150

9 Answers9

67

With iOS 11, Apple provided 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-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.

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

Enthouan
  • 716
  • 6
  • 7
  • Hello Enthouan, I have installed iOS 11 SDK and tried your approach but I'm getting a linker error for some reason when I try to use NEHotspotConfigurationManager or NEHotspotConfiguration. Namely: "_OBJC_CLASS_$_NEHotspotConfigurationManager", referenced from: objc-class-ref in HomeViewController.o. If you have experienced any similar problems, I would really appreciate your help. – Olexiy Burov Sep 13 '17 at 23:04
  • Hey Olexiy, did you enable the Hotspot Capability? if it doesn't make sure NetworkExtension.framework is linked with your binary. – Enthouan Sep 14 '17 at 00:04
  • Yes I enabled that and I checked that NetworkExtension.framework is linked correctly. At least I'm able to use NEHotspotHelper class, but not the other ones. – Olexiy Burov Sep 14 '17 at 00:07
  • In your project settings underBuild Settings > Base SDK, is it set to iOS 11 or Latest iOS? – Enthouan Sep 14 '17 at 00:24
  • I tried both, didn't make a difference :(. Also I'm trying to run it on the simulator, could that be the issue ? – Olexiy Burov Sep 14 '17 at 00:28
  • That was going to be my next guess, yep you need to try with an actual device. Not quite sure why it causes linking issue with the simulator though, you should fill a radar ;). – Enthouan Sep 14 '17 at 00:30
  • 1
    Yeah, guess what I needed to connect an actual device. I hesitated to do it initially because it would require me to install iOS 11 Beta on my phone, but if there is no other way - I'm fine with this. Upvoted. – Olexiy Burov Sep 14 '17 at 02:58
  • Is the joinOnce option a secure way to do this or could it be possible to read the wifi pass on a jailbroken phone while the app is open? – user3154108 Nov 10 '17 at 13:32
  • I cannot see Hotspot capability in list of capabilities. Aby help on this? – Mohamad Bachir Sidani Dec 25 '17 at 14:45
  • 2
    does this require Apple approval, any entitlements? – anoop4real Mar 19 '18 at 06:08
  • You also need to add "import NetworkExtension" to be able to use NEHotspotConfiguration. – Mikrasya Jun 08 '20 at 07:14
28

connect wifi networks in iOS 11. You can connect wifi using ssid and password like following.

Enable Hotspot on App Id configure services

Enable Hotspot on App Id configure services

After Enable Hotspot Configuration

After Enable Hotspot Configuration

Swift 4.0 Code for iOS 11 Only:

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")
    }
}
JackieNBee
  • 173
  • 3
  • 18
Virajkumar Patel
  • 1,533
  • 12
  • 19
21

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.

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 thread for serving it (but people don't find it because it's not specifically about WiFi), and this blog for querying if it's installed which is surprisingly nontrivial but does indeed work.

I've answered this question many 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:

var config = new NEHotspotConfiguration(Ssid, Pw, false);
config.JoinOnce = false;
Eliot Gillum
  • 832
  • 9
  • 18
  • 1
    I voted to close the other question, and leave this one open because this one has more information, and has an authenticated user who can accept an answer. – Senseful Jun 24 '17 at 23:46
9

Short answer, no.


Long answer :)

This question was asked many times:

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.

Community
  • 1
  • 1
filaton
  • 2,257
  • 17
  • 27
  • Even when using a 3rd Party Library? Is there a way to ask a user to join a network then? Like, pop up the dialog to join a network then the user would input the values there manually..? – Razgriz Mar 30 '16 at 08:43
  • I'm not aware of any 3rd Party Lib doing that can help you. Usually, I think you would check for Wi-Fi/cellular availability (as described [there](http://stackoverflow.com/questions/35717944/ios-swift-prompt-dialog-that-opens-cellular-wifi-connection-settings)) and redirect the user to Settings application using `UIApplicationOpenSettingsURLString`. – filaton Mar 30 '16 at 09:01
  • 1
    this answer is now out of date – pstanton Sep 21 '18 at 00:12
7

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
            }
    }
iHarshil
  • 739
  • 10
  • 22
  • Yeah. in objc or swift. Once connection is established, how to send packets to the connected network and how to receive the packets from the network??? – Abdul Yasin Jun 27 '18 at 07:19
  • 1
    Abdul once your connected, you send and recieve same as any other. Just google how to do network programming, its a topic far too wide for a single Stack Overflow post. – Shayne Jun 05 '19 at 07:43
5

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

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
    }
Amal T S
  • 3,327
  • 2
  • 24
  • 57
ingconti
  • 10,876
  • 3
  • 61
  • 48
4

We can programmatically connect wifi networks after IOS 11. 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 accured
                print(error?.localizedDescription)
            }
            else {
                //success
            }
        }
Savas Adar
  • 4,083
  • 3
  • 46
  • 54
0

You can connect to wifi using Xcode 9 and swift 4

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)
    }
Mayur Rathod
  • 361
  • 3
  • 13
0

I'm not sure if this will help anyone but there IS a Wifi QR code that Apple put into iOS since version 9 if I'm not mistaken. It's been there a while.

You can go here: https://qifi.org

Put your SSID and password in. Print out the QR code or just keep it on a screen. Point the iPhone at the QR code using the camera app. You will get a notification bar that if you press will setup your WIFI connection on the fly.

This 'should' work with Android but I haven't test it yet. It DOES work with iOS.

The nice thing with this solution is you can make your WIFI base 'hidden' on the network and folks can still connect to it which is very desirable in a high traffic area for security reasons.

Waxhaw
  • 599
  • 5
  • 9