인터넷 연결을위한 탭이 두 개 있고 Reachability
클래스 인 tabBarController가 있습니다. AppDelegate
의 didFinishLaunching
있어서 내부 Swift2 iOS 도달 가능성 클래스 및 NSNotificatonCenter가 브로드 캐스트를 수신하지 않습니다.
NSNotification
방송되는지 확인하기 위하여 현재 네트워크 연결을 확인하는
checkReachability
기능을 갖는다.
두 번째 탭 안에는 레이블과 내부가 있습니다. viewDidLoad
방송을 청취하는 2 명의 관찰자가 있습니다. 또한 현재 네트워크 연결을 기반으로 레이블 텍스트를 변경하는 두 가지 기능 (관찰자에 의해 트리거 됨)이 있습니다.
시뮬레이터를 사용하고 있고 첫 번째 또는 두 번째 탭에서 Wi-Fi를 켜기/끄기로 전환 할 때 두 번째 탭의 라벨 텍스트가 변경되지 않습니다.
왜 텍스트가 변경되지 않고 왜 관찰자는 응답하지 않습니까?
도달 가능성 클래스 : 는 참고로 나는이 유튜브 VID에서 도달 가능성 클래스가 있고 난 스위프트 2.3 스위프트 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
}
}
}
AppDelegate에 :
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)
}
}
Bumberg 조언과 HNY에 감사드립니다! 이 비디오를보십시오. 그는 tabBar를 사용하지 않고 있지만 didFinish (최소 12시 9 분)에 checkStatus 함수를 설정하고 다른 vc의 viewDidLoad (단 하나의 vc 앱은 탭이 아님)에 구독자가 있습니다. 그가 wifi를 켜거나 끌 때 잘 작동합니다 (분 29:35). 내 두 번째 탭이 아직 만들어지지 않은 상태에서 일단 탭하면 살아있는 것입니다. 그 시점에서 와이파이를 켜고 끄지 않아야합니다. 또한 레이블이있는 것처럼 동일한 구독자 코드를 tabOne에 넣습니다. 시작시 즉시로드되고 아무 것도 발생하지 않습니다. https://youtu.be/BlBhHgoW9wM - –