[ad_1]
I’ve a Viewcontroller VC_B the place I hook up with Wifi through the use of “NEHotspotConfigurationManager.shared” to a particular gadget. This operate presently errors as a result of it would return nil at any time when success or failure. Please observe these hyperlinks beneath.
https://developer.apple.com/boards/thread/109412
https://stackoverflow.com/…/nehotspotconfigurationmanag...
So I’ll create a Viewcontroller VC_A, then use “pathUpdateHandler” to catch occasions at any time when the wifi connection modified. Then save wifi SSID to CURENT_SSID through the use of CoreLocation.
- entry gadget wifi by DEVICE_SSID at VC_B.
- wait till CURENT_SSID efficiently is up to date at VC_A.
- evaluate DEVICE_SSID and CURRENT_SSID.
1. VC_A
// get present SSID and save to UserData.shared.current_wifi_ssid
var ssid: String {
var return_ssid:String = "取得できませんでした。"
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary?,
let ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String {
return_ssid = ssid
}
}
}
UserData.shared.current_wifi_ssid = return_ssid
return return_ssid
}
//catch when having wifi connection altering occasion
func checkNetWorking() {
monitor.pathUpdateHandler = { path in
if path.standing == .happy {
self.isConnected = true
Backend.shared.checkSignedIn {(isSignedIn: Bool) in
if !isSignedIn {
DispatchQueue.principal.async {
AlertHelper.displayOK(self, title: "エラー", message: "サインインしてください")
}
}
}
print("ssid:(self.ssid)")
} else {
//############# location - wifi
print("ssid:(self.ssid)")
let str = self.ssid // 0123456789
let startIndex = str.index(str.startIndex, offsetBy: 0)
let endIndex = str.index(str.startIndex, offsetBy: 6)
let substring = String(str[startIndex...endIndex]) // 0123456
if(substring == "THETAYN"){
print("THETA connecting, ignore エラー popup")
}
else {
DispatchQueue.principal.async {
AlertHelper.displayOK(self, title: "エラー", message: "ネットワーク通信が確認できません。圏外、もしくはwifiに正しく接続されているかご確認ください。")
}
}
//#############
self.isConnected = false
self.isNeedUpdated = true //20211204 まだ利用していない
}
}
}
2. VC_B
//hook up with gadget wifi and test
func connectTheta(ID: String){
TableVIew.isUserInteractionEnabled = false
SVProgressHUD.present()
let supervisor = NEHotspotConfigurationManager.shared
let ssid = "THETAYN" + ID + ".OSC"
let password = ID
let isWEP = false
let hotspotConfiguration = NEHotspotConfiguration(ssid: ssid, passphrase: password, isWEP: isWEP)
hotspotConfiguration.joinOnce = true
hotspotConfiguration.lifeTimeInDays = 1
UserData.shared.current_wifi_ssid = "NULL"
supervisor.apply(hotspotConfiguration) { (error) in //error all the time nil
if error != nil {
// error
} else {
// success
}
check_wifi_theta();
func check_wifi_theta(){
//i am utilizing whereas right here to attend activity from VC_A
whereas(UserData.shared.current_wifi_ssid=="NULL"){
Thread.sleep(forTimeInterval: 0.5)
}
print("ssid theta", ssid)
print("ssid present", UserData.shared.current_wifi_ssid)
if (UserData.shared.current_wifi_ssid==ssid){
print("join Theta OK")
AlertHelper.displayOK(self, title: "接続できました", message: "", okHandler: {_ in
self.navigationController?.popViewController(animated: true)
})
}
else{
AlertHelper.displayOK(self, title: "接続できません", message: "THETAの電源が入っている確認してください。")
}
self.TableVIew.isUserInteractionEnabled = true
SVProgressHUD.dismiss()
}
}
}
I’m caught at step 2 when making an attempt to attend for “pathUpdateHandler” to complete from VC_B. Typically it modifications however generally it is not.
I exploit whereas loop right here and it is solved my downside.
However I feel it is will not be your best option. Is there any higher resolution for this? Thanks!
[ad_2]
