[iOS 10] 接続中のWi-FiのSSIDを取得するCNCopySupportedInterfacesは、iOS 10で復活していた

2016.11.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

1 CNCopySupportedInterfaces

接続中のWi-FiのSSID等の情報を取得するには、下記のようなコードを使用します。

import UIKit
import NetworkExtension
import SystemConfiguration.CaptiveNetwork

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let interfaces = CNCopySupportedInterfaces()
        let count = CFArrayGetCount(interfaces)
        if count > 0 {
            print("Count=\(count)")
            let interfaceName: UnsafePointer<Void> = CFArrayGetValueAtIndex(interfaces, 0)
            let rec = unsafeBitCast(interfaceName, AnyObject.self)
            let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
            if unsafeInterfaceData != nil {
                let interfaceData = unsafeInterfaceData as Dictionary!
                let ssid = interfaceData["SSID"] as! String
                let bssid = interfaceData["BSSID"] as! String
                print("SSID=\(ssid) BSSID=\(bssid)")

            }
        }
    }
}

出力は下記の通りです。

Count=1
SSID=spw05 BSSID=0:90:fe:e6:80:ef

2 deprecated

しかし、先のコードは、Xcode 7.3.1でiOS 9のSDKを使用すると、次のようにdeprecatedのwarningが出ます。 一応、動作しますが、今後は、NEHotspotHelperを使用するようにと当時アナウンスされていた記憶しています。

003

3 iOS 10で復活

しかし、Xcode 8. 1(8B62)で同じコード(Use Legacy Swift Language VersionをYESに設定)を開くと、SDKを8,9,10を変えてみても、deprecatedは、表示されません。

また、下記の端末で確認してみましたが、どれも同じ出力になっていました。

  • iPhone 6 10.0.1(14A403)
  • iPhone 5S 9.3.6(13G36)
  • iPhone 5S 8.1.3(12B466)

Referenceにも、現在、deprecatedは、表記されていないようです。
API Reference CNCopySupportedInterfaces
API Reference CNCopyCurrentNetworkInfo

004

4 最後に

という事で、iOS 10以降でも、SSIDの情報取得は、特にAppleに申請しなくても大丈夫みたいです。