gpt4 book ai didi

ios - Swift2 iOS Reachability Class 和 NSNotificatonCenter 未接收广播

转载 作者:行者123 更新时间:2023-11-29 00:32:27 25 4
gpt4 key购买 nike

我有一个带有 2 个选项卡的 tabBarController 和一个用于互联网连接的 Reachability 类。

AppDelegatedidFinishLaunching 方法中,我有一个 checkReachability 函数,用于检查当前网络连接以查看 wifi 是否打开或关闭在任何一种情况下,它都会向第二个选项卡广播 NSNotification

在第二个选项卡内有标签,在 viewDidLoad 内有 2 个观察者在监听广播。我还有 2 个函数(由观察者触发)会根据当前网络连接更改标签文本。

我正在使用模拟器,当我从第一个或第二个选项卡打开/关闭 wifi 时,第二个选项卡的标签文本永远不会改变。

为什么文本没有变化,为什么观察者没有反应?

可达性等级:仅供引用,我从这个 youtube 视频中获得了可达性类,并将其从 Swift 3 切换到 Swift 2.3:https://www.youtube.com/watch?v=IlsfXjESatg&t=532s

import Foundation
import UIKit
import SystemConfiguration

protocol Utilities {
}

extension NSObject:Utilities{

enum ReachabilityStatus {
case notReachable
case reachableViaWWAN
case reachableViaWiFi
}

var currentReachabilityStatus: ReachabilityStatus {

var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))

zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return .notReachable
}

var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return .notReachable
}

if flags.contains(SCNetworkReachabilityFlags.Reachable) == false{
// The target host is not reachable.
return .notReachable
}
else if flags.contains(SCNetworkReachabilityFlags.IsWWAN) == true{
// WWAN connections are OK if the calling application is using the CFNetwork APIs.
return .reachableViaWWAN
}

else if flags.contains(.ConnectionRequired) == false {
// If the target host is reachable and no connection is required then we'll assume that you're on Wi-Fi...
return .reachableViaWiFi
}
else if (flags.contains(.ConnectionOnDemand) == true || flags.contains(.ConnectionOnTraffic) == true) && flags.contains(.InterventionRequired) == false {
// The connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs and no [user] intervention is needed
return .reachableViaWiFi
}
else {
return .notReachable
}
}
}

应用委托(delegate):

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.checkReachability()

return true
}
//This function is declared inside AppDelegate
func checkReachability(){
if currentReachabilityStatus == .reachableViaWiFi {
NSNotificationCenter.defaultCenter().postNotificationName("wifi", object: nil)
}else if currentReachabilityStatus == .reachableViaWWAN{
print("WWAN.")
}else{
NSNotificationCenter.defaultCenter().postNotificationName("noWifi", object: nil)
}
}

第二个标签:

class SecondTabController: UIViewController {

@IBOutlet weak var labelTwo: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(wifiConnection), name: "wifi", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(noConnection), name: "noWifi", object: nil)
}

func wifiConnection(){
self.labelTwo.text = "Wifi Connection"
}

func noConnection(){
self.labelTwo.text = "No Connection"
}

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

最佳答案

您仅将通知发布到通知中心一次,即在应用程序启动时:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Your method `checkReachability` will post notifications:
self.checkReachability()
return true
}

但是您的 SecondTabController 尚未创建,因此尚未订阅接收您在应用程序启动时发送的通知。通知发布后,任何订阅者都会立即收到通知。通知“不要等到”稍后添加某个订阅者。

作为可达性管理器,您可能需要什么,它会在 WiFi 可达性发生变化时继续运行并向通知中心发布通知。

您可能想要使用现有的可达性管理器,such as Alamofire's NetworkReachabilityManager as described in this question .

关于ios - Swift2 iOS Reachability Class 和 NSNotificatonCenter 未接收广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414737/

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