ukSeung iOS

[Swift] Hotspot Configuration, 핫스팟 연결 본문

iOS/Swift

[Swift] Hotspot Configuration, 핫스팟 연결

욱승 2023. 5. 24. 12:58

안녕하세요 욱승임다ㅎ

이번 포스팅에서는 기기간 핫스팟 연결을 구현 해보겠읍니당

 

요즘은 공감 못 할 제목 ㅋ..

 

Capability

해당 Capability를 추가

 

작동 방식

'ConnectToHotspot' 버튼을 누르면 핫스팟 연결 요청하는 방식

 

연결성공

 

켜놓은 핫스팟에 한대라도 연결되면 상단 표시줄에 링크 모양이 노출

 

소스코드

 

View

//
//  HotspotConfigurationViewController.swift
//  SwiftPractice
//
//  Created by ukseung.dev on 2023/05/22.
//

import Foundation
import UIKit
import RxSwift

final class HotspotConfigurationViewController: UIViewController, UIViewControllerAttribute {
    
    let viewModel = HotspotConfigurationViewModel()
    let disposeBag = DisposeBag()
    
    var navTitle: String?
    
    lazy var button = UIButton(type: .system).then {
        $0.setTitle("connectToHotspot", for: .normal)
    }
    
    // getWiFiInformation 로그인 성공 후 UILabel.text
    lazy var wifiInfoLabel = UILabel().then {
        $0.numberOfLines = 0 // 여러 줄
        $0.sizeToFit()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBar()
        setUI()
        setAttributes()
        bindRx() // 미사용 함수
    }
    
    func setNavigationBar() {
        self.navigationItem.title = navTitle ?? ""
    }
    
    func setUI() {
        self.view.backgroundColor = .white
        self.view.addSubview(button)
        self.view.addSubview(wifiInfoLabel)
    }
    
    func setAttributes() {
        button.snp.makeConstraints {
            $0.top.equalTo(self.view.safeAreaLayoutGuide).offset(30)
            $0.left.equalTo(30)
            $0.right.equalTo(-30)
            $0.height.equalTo(50)
        }
        
        wifiInfoLabel.snp.makeConstraints {
            $0.left.equalTo(20)
            $0.right.equalTo(-20)
            $0.top.equalTo(button.snp.bottom).offset(40)
            $0.height.equalTo(50)
        }
    }
    
    func bindRx() {
        button.rx.tap
            .subscribe(onNext: viewModel.connectToHotspot)
            .disposed(by: disposeBag)
        
        viewModel.inCompleted
            .bind(to: wifiInfoLabel.rx.text)
            .disposed(by: disposeBag)
    }
}

 

 

ViewModel

//
//  HotspotConfigurationViewModel.swift
//  SwiftPractice
//
//  Created by ukseung.dev on 2023/05/22.
//

import Foundation
import NetworkExtension // 핫스팟 import
import RxSwift
import RxCocoa

final class HotspotConfigurationViewModel {
    
    let inCompleted = PublishRelay<String>()
    
    func connectToHotspot() {
        let hotspotConfig = NEHotspotConfiguration(ssid: "욱", passphrase: "1234567890", isWEP: false) // 내 기기 핫스팟 하드코딩
        hotspotConfig.joinOnce = true
        
        NEHotspotConfigurationManager.shared.apply(hotspotConfig) { error in
            if let error = error {
                print("핫스팟 연결 실패")
                self.inCompleted.accept("Failed to connect to Wi-Fi hotspot: \(error.localizedDescription)")
            } else {
                print("핫스팟 연결 성공")
                self.inCompleted.accept("Successfully connected to Wi-Fi hotspot")
            }
        }
    }
}

 

ps. 핫스팟이 미리 켜져있어야함 !

GitHub

 

GitHub - shinseunguk/SwiftPractice: SwtiftPractice

SwtiftPractice. Contribute to shinseunguk/SwiftPractice development by creating an account on GitHub.

github.com

 

728x90
반응형