如何快速打印当前的本地网络名称 [英] How to print the current local network name in swift

查看:58
本文介绍了如何快速打印当前的本地网络名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 iOS 应用程序,它在屏幕顶部显示当前的本地网络名称,等等.我正在解决显示此问题的不同方式,但我无法管理当前程序.有人可以帮我吗?

I am creating an iOS app which displays the current local network name at the top of the screen, and so forth. I am trouble-shooting different ways to display this but I can't manage the current program. Can someone help me out?

我查看了一些 GitHub、堆栈溢出和 youtube 对此的评论,但没有一个有效.

I've looked at several GitHub, stack overflow, and youtube comments about this, but nome of them worked.

在我当前使用的 Xcode(10.4.2) 中,我使用标签(如果我应该使用其他东西,请纠正我)来显示当前名为 -->(WiFi:......)

In the current Xcode I'm using which is Xcode(10.4.2) I'm using a label(correct me if I should use something else) to display the current Wifi named --> (WiFi: ......)

推荐答案

请不要在模拟器上测试,用iphone测试.

Please don't test on the simulator, use the iphone for testing.

导入系统配置:

import SystemConfiguration.CaptiveNetwork

在 ViewDidLoad 中:

In ViewDidLoad :

let wifiName = getWiFiName()
print("Wifi: \(String(describing: wifiName))")

功能:

func getWiFiName() -> String? {

    var serviceSetIdentifier:String?
    if let interfaces = CNCopySupportedInterfaces() as Array? {
        interfaces.forEach { interface in
            guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? else { return }
                serviceSetIdentifier = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
            }
    }
    return serviceSetIdentifier
}

这篇关于如何快速打印当前的本地网络名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆