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?

9 Answers 9

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

11
  • 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. Sep 13, 2017 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, 2017 at 0: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. Sep 14, 2017 at 0:07
  • 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. Sep 14, 2017 at 2:58
  • 2
    does this require Apple approval, any entitlements?
    – anoop4real
    Mar 19, 2018 at 6:08
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")
    }
}
6
  • 9
    If you have a typo in SSID, your connection fails and there is no error, it still prints "Connected".
    – Willjay
    Dec 8, 2017 at 7:01
  • 2
    Is there a way to know if a SSID is in range of the iPhone?
    – razvan
    Dec 19, 2017 at 12:02
  • i cant see hotspot as capability in my capabilites. any idea why? Dec 25, 2017 at 14:48
  • @MohammadBashirSidani to see hotspot as capability you need to enroll in Apple Development program. More info you can find here developer.apple.com/support/app-capabilities Feb 26, 2018 at 13:48
  • 1
    @Willjay this issue is also discussed here stackoverflow.com/questions/53693025
    – DerSeegler
    Nov 14, 2019 at 12:34
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;
1
  • 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, 2017 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.

3
  • 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, 2016 at 8: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) and redirect the user to Settings application using UIApplicationOpenSettingsURLString.
    – filaton
    Mar 30, 2016 at 9:01
  • 1
    this answer is now out of date
    – pstanton
    Sep 21, 2018 at 0: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
            }
    }
2
  • 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??? Jun 27, 2018 at 7: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 5, 2019 at 7: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
    }
1
  • TARGET_IPHONE_SIMULATOR is deprecated, use TARGET_OS_SIMULATOR instead.
    – Nilesh
    Jun 19, 2018 at 6:22
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
            }
        }
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)
    }
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.