2016-12-27 44 views
1

如何知道在ios中與設備連接的網絡主機。 我想在Swift中學習如何檢查我們連接的網絡。如何知道在IOS中與設備連接哪個網絡主機

+1

使用可達性框架,它將有助於解決您的問題 –

+0

[可達性](https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html)來自蘋果 – byJeevan

+0

AFNetworking也幫助,其中包括可達性 – Pushp

回答

1

使用這個自定義類來獲取到其當前連接到WiFi網絡: -

import Foundation 
import SystemConfiguration.CaptiveNetwork 

public class SSID { 

    class func fetchSSIDInfo() -> String { 

     var currentSSID = "" 
     if let interfaces = CNCopySupportedInterfaces() { 
      for i in 0..<CFArrayGetCount(interfaces) { 
       let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i) 
       let rec = unsafeBitCast(interfaceName, to: AnyObject.self) 
       let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString) 
       if unsafeInterfaceData != nil { 
        let interfaceData = unsafeInterfaceData! as NSDictionary! 
        print(interfaceData) 
        currentSSID = interfaceData?.value(forKey:"SSID") as! String 
       } 
      } 
     } 
     return currentSSID 
    } 
} 

你可以得到的信息是這樣的: -

print(SSID.fetchSSIDInfo()) 
相關問題