[SOLVED] Is it possible to connect to WiFi automatically by capturing QR-Code?


I’m newbie in iOS development(SwiftUI).

Recently, I have encountered a problem about connecting to WiFi by capturing QR-Code.
I wonder that if is there any possible solution to connect to WiFi directly by using QR-Code.
After I did a lot of research, I still cannot find any references about this issue.
There are my keywords: SwiftUI, Swift, QR-Code, Wi-Fi, iOS.

What do I have now:


struct ContentView: View {
    @State private var isShowingScanner = false
    @State private var resultOfScanning: String = "Result will be shown here..."
    var body: some View {
        VStack {
            Button(action: { self.isShowingScanner = true }, label: { Text("Scan Button") })
                .sheet(isPresented: self.$isShowingScanner) {
                    CodeScannerView(codeTypes: [.qr], simulatedData: "www.opgg.com", completion: self.handleScan)

    func handleScan(result: Result<String, CodeScannerView.ScanError>) {
        self.isShowingScanner = false
        switch result {
        case .success(let code):
            let codeStr = code as! String
            self.resultOfScanning = codeStr
        case .failure(let error):
            print("Scanning failed")

CodeScannerView (Reference: An article by Paul Hudson)

What do I wanna build:
I want to use this App, connecting to the specific WiFi automatically by capturing QR-Code(It contains SSID & Password of WiFi).

What is my problem:
I cannot find a way to connect to the specific WiFi automatically by using QR-Code. It seems that no one have talked about this issue.

May someone know that how to solve the problems like above-mentioned?

Thanks for comments and answers.


We have four steps to deal with this issue.

1.Capture the QR-Code which contains the specific WiFi hotspot information(SSID, Password, Encryption Type).

2.Convert JSON data of QR-Code to Dictionary.

3.Get the SSID, Password and Encryption type value from before-mentioned Dictionary.

4.Use NEHotspotConfiguration of Apple API to set our SSID, Password and Encryption type and connect to the specific WiFi Hotspot.

p.s. Your app needs the signing certificate to active some function of your project and Apple API.

Let’s see what have I done here:

func handleScan(result: Result<String, CodeScannerView.ScanError>) {
switch result {
case .success(let code):
    let data_code = code.data(using: .utf8)
    do {
        let dict_code = try JSONSerialization.jsonObject(with: data_code!, options: .allowFragments) as! [String : Any]
        let wifi_ssid = dict_code["S"] as! String
        let wifi_pwd = dict_code["P"] as! String
        let wifi_type = dict_code["T"] as! String

        let configuration = NEHotspotConfiguration.init(ssid: wifi_ssid, passphrase: wifi_pwd, isWEP: self.checkWifiType(type: wifi_type))
        configuration.joinOnce = true
        NEHotspotConfigurationManager.shared.apply(configuration) {
            (error) in
            if error != nil {
                if let errorStr = error?.localizedDescription {
                    print("Error Information:\(errorStr)")
                if (error?.localizedDescription == "already associated.") {
                } else {
                    print("No Connected!")
            } else {

    } catch (let error) {
        print("JSONSerial... Convert Error:\(error.localizedDescription)")

case .failure(let error):
    self.connectionStatus = "Scanning failed!"

After doing this, I finally can scan my own QR-Code and connect to the specific WiFi Hotspot.

Answered By – Spencer Reid

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *