2015-07-30 12 views
15

Desidero ottenere tutte le reti WiFi disponibili in una regione e il loro valore SSID. Ma il problema è come ottenere il SSID di tutta la rete WiFi disponibile anche se non sono connesso a uno.Come ottenere il nome della rete wifi disponibile in iOS usando swift

+0

@EnricoSusatyo ho lo stesso problema che non posso avere ottenere tutta la lista wifi disponibili. –

+0

Non facilmente. Scansionare le reti wifi è una minaccia alla sicurezza. – Sulthan

+3

Ho trovato domande simili fatte su SO con una rapida ricerca su google, la risposta comune è che, mentre è possibile, non si dovrebbe a causa delle regole delle mele – RubberDucky4444

risposta

1

Ecco la mia classe che stampa il nome della rete Wi-Fi

import UIKit 
import Foundation 
import SystemConfiguration.CaptiveNetwork 

class FirstView: UIViewController 
{ 
    @IBOutlet weak var label: UILabel! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     let ssid = self.getWiFiName() 
     print("SSID: \(ssid)") 
    } 

    func getWiFiName() -> String? { 
     var ssid: String? 
     if let interfaces = CNCopySupportedInterfaces() as NSArray? { 
      for interface in interfaces { 
       if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { 
        ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String 
        break 
       } 
      } 
     } 
     return ssid 
    } 
} 
+2

Questo sembra solo mostrare l'interfaccia SSID collegato. – picciano

+0

'var ssid =" "; (CNCopySupportedInterfaces() come? [CFString]) ?. forEach ({ ssid = (CNCopyCurrentNetworkInfo ($ 0) as? [String: Any])? [KCNNetworkInfoKeySSID as String] as? String ?? ""}) ' –

6

Primo;

importazione SystemConfiguration.CaptiveNetwork

Poi;

func getInterfaces() -> Bool { 
    guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else { 
     print("this must be a simulator, no interfaces found") 
     return false 
    } 
    guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else { 
     print("System error: did not come back as array of Strings") 
     return false 
    } 
    for interface in swiftInterfaces { 
     print("Looking up SSID info for \(interface)") // en0 
     guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else { 
      print("System error: \(interface) has no information") 
      return false 
     } 
     guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else { 
      print("System error: interface information is not a string-keyed dictionary") 
      return false 
     } 
     for d in SSIDDict.keys { 
      print("\(d): \(SSIDDict[d]!)") 
     } 
    } 
    return true 
} 
+1

Questo sembra solo restituire il wifi attualmente connesso almeno su iOS 11.2.2 su un iPhone 7 – DCIndieDev

Problemi correlati