- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 2 个选项卡的 tabBarController 和一个用于互联网连接的 Reachability
类。
在 AppDelegate
的 didFinishLaunching
方法中,我有一个 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/
我在开发 iPhone 应用程序时遇到了一次非常奇怪的崩溃。似乎每次我向 friend 展示我的应用程序时它都会崩溃,但否则它永远不会崩溃。在对墨菲定律感到困惑之后,我确定了事故的模式——纽约地铁。使
我正在创建一个 iPhone 应用程序,我正在使用 RestKit api 连接到服务器。每当我将 Reachability.m 和 Reachability.h 添加到项目的第二个 UIViewCo
我使用了Andrew's修改后的Reachability类。 -(void)viewDidLoad [[NSNotificationCenter defaultCenter] postNotifica
我正在使用更新的 Reachability库来测试互联网连接是否可达。我尝试记录一条消息以防无法访问互联网,但日志不调试: //Test the internet connection Reachab
我使用 Reachability 检查我的应用程序的网络状态,除了在 iOS 9.0.1 或更高版本中 reachableBlock 和 unreachableBlock 被调用两次外,一切正常,这给
我需要在我的 iPhone 项目中测试网络可达性。使用哪个项目更好? Reachability与 UIDevice-Reachability 最佳答案 可达性(如果您唯一的问题是测试网络可达性)。这是
我看到论坛上有很多关于Apple的Reachability Class的条目,但我还没有看到这个的解决方案。 我正在开发一个 VoIP 应用程序,需要在后台模式下实时检测连接变化。 SCNetwork
使用 Reachability Class(来自 Apple)来检查远程主机的可用性是否明智?例如,www.google.com 或者我应该使用 NSString *connectedString =
我试图将使用 rebar 构建的 REST API 示例转换为 rebar3 版本( http://jordenlowe.com/title/Explain_like_I_am_5_-_Erlang_
我正在尝试将 Reachability.swift 更新到 swift 3.0,但在将 Reachability 实例传递给回调函数时遇到问题。 这是我的片段:* 请注意 self = 可达性等级 v
我在代码中使用了 Apple 的可达性类。下面是appdelegate的代码 NSNotificationCenter.defaultCenter().addObserver(self
有时我必须检查互联网连接并为此使用“可达性”。 最好使用“可达性”手动检查互联网连接,例如: BOOL status = ([[Reachability reachabilityForInternet
我在这个应用程序的几个 View 中使用了 AFNetworking Reachability。一种观点认为,存在奇怪的问题。在每个 View 中,我都在 loadView 中开始监控:[[AFNet
在 CI 系统中,我使用 valgrind 运行一堆测试,如果 valgrind 没有发现错误,我期望返回值 0,否则返回 1。测试本身成功运行并返回 0。 这就是 error-exitcode 的用
我想在我的应用程序中使用 ReachabilitySwift 类。我在我的 podFile 中添加了依赖项,我从 github 获取了代码. 我遇到了这个错误: 不能在这一行调用不带参数的 'Reac
我正在尝试在我的应用中实现可达性。我在 applicationDidFinishLaunching 中有它。如果我的连接真的很糟糕并且我启动了应用程序,Reachability 会一直持续并且经常使应
我正在使用 Apple 提供的可达性示例代码来检查网络连接并获得更改通知 (Reachability Sample code)。 我查看了我的应用程序的一些崩溃锁。似乎有些崩溃是由于 Reachabi
Apple 名为 Reachability 的示例应用展示了如何检测连接。如果您只有 wifi 而没有互联网,该应用会在下面的第二行停顿一分钟以上: SCNetworkReachabilityFlag
我目前正在使用此检查 viewDidLoad 上的网络连接: -(BOOL)reachable { ReachabilityDRC *r = [ReachabilityDRC reachabi
使用 Reachability 比下面的代码有什么优势?我觉得 Reachability 的代码量很大,但如果它在任何方面都更好,那么我会改用它。 NSString *connectionString
我是一名优秀的程序员,十分优秀!