gpt4 book ai didi

ios - Swift iOS-有wifi连接但没有互联网连接怎么办?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:37 26 4
gpt4 key购买 nike

我在我的 iOS 应用程序中使用 Alamofire。我在 viewWillAppear 和带有 NSNotifications 的 AppDelegate 中使用 bool 值来检查是否有互联网连接。如果没有 wifi 连接,则会出现一个弹出窗口通知用户。如果有 wifi 连接,弹出窗口消失,一切正常。只要 wifi 明显无法正常工作,我就没有遇到任何问题。

我在一次聚会上有人向我解释说它的工作方式是寻找 wifi 连接而不是互联网连接。例如,如果我有一个 wifi 路由器并且它已插入但路由器未连接到互联网,Alamofire 会将此视为成功连接,因为它实际上正在连接到 wifi,尽管它不知道 wifi 无法连接上网。

我只是处于连接到开放网络的情况,我的应用程序最初的响应就像我实际上连接到互联网一样(没有弹出窗口),但我无法连接到任何东西。无线网络信号是满的。在终端中,我运行了一个 ping 命令,结果发现连接已断开。我的应用程序无法区分。

enter image description here enter image description here

如何在这种情况下显示弹出窗口?

还有什么是 .case unknown 的?

Alamofire.Swift:

import Foundation
import Alamofire

open class NetworkManager {

open static var sharedManager: NetworkReachabilityManager = {

let reachabilityManager = NetworkReachabilityManager()

reachabilityManager?.listener = { (status) in

switch status {

case .notReachable:
print("The network is not reachable")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "unsuccessful"), object: nil)

case .unknown : //???????
print("It is unknown wether the network is reachable")
//I'm not sure whether to put a Notification for successful or unsuccessful???

case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)

case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)
}
}

reachabilityManager?.startListening()
return reachabilityManager!
}()
}

应用委托(delegate):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

NetworkManager.sharedManager.startListening()

一些VC:

override func viewWillAppear() {
super.viewWillAppear()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(successful), name: "successful", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(unsuccessful), name: "unsuccessful", object: nil)

if NetworkManager.sharedManager.isReachable == true{
self.successful()
}else{
self.unsuccessful()
}

if NetworkManager.sharedManager.isReachableOnWWAN == true{
self.successful()
}else{
self.unsuccessful()
}

if NetworkManager.sharedManager.isReachableOnEthernetOrWiFi == true{
self.successful()
}else{
self.unsuccessful()
}
}

func successful(){
//dismiss pop up
}

func unsuccessful(){
//show pop up
}

deinit{
NSNotificationCenter.defaultCenter().removeObserver(self, name: "successful", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "unsuccessful", object: nil)
}
}

最佳答案

您可以使用主机初始化 NetworkReachabilityManager,例如 google 主机,因为默认值为 0.0.0.0

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

当您开始监听可达性管理器对主机执行 ping 操作时。如果网络可用,您可以缓存 SSID 并在 SSID 更改时再次 ping。

对于 case .unknown 最好放一个不成功的通知。

获取 SSID 的示例(在模拟器中不起作用):

func fetchSSIDInfo() ->  String? {  
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 let unsafeInterfaceData = unsafeInterfaceData as? Dictionary<AnyHashable, Any> {
return unsafeInterfaceData["SSID"] as? String
}
}
}
return nil
}

关于ios - Swift iOS-有wifi连接但没有互联网连接怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43067753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com