在Swift中是否有办法AFNetworking
可达性将每秒不断检查互联网连接,到目前为止这是我所拥有的,它只检查一次:
override func viewDidLoad() {
AFNetworkReachabilityManager.sharedManager().startMonitoring()
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock{(status: AFNetworkReachabilityStatus?) in
switch status!.hashValue {
case AFNetworkReachabilityStatus.NotReachable.hashValue:
print("no internet")
case AFNetworkReachabilityStatus.ReachableViaWiFi.hashValue,AFNetworkReachabilityStatus.ReachableViaWWAN.hashValue:
print("with internet")
default:
print("unknown")
}
}
}
如何连续检查互联网连接?
答案 0 :(得分:4)
AFNetworking可达性确实持续检查连接,如果我的一些应用程序能够很好地使用它,我就会使用它。如果您的代码无效,我相信可能是因为您在使用startMonitoring
设置处理程序之前调用setReachabilityStatusChangeBlock
,因此您可能会错过初始事件。此外,与您的问题无关但您可以进行改进,您不需要在switch语句中使用hashValue
,您可以直接使用status
,并且您可以获得Swift编译器检查case
语句的完成情况。总之,请尝试使用以下版本的代码,看看它是否有效:
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
switch status {
case .NotReachable:
print("Not reachable")
case .ReachableViaWiFi, .ReachableViaWWAN:
print("Reachable")
case .Unknown:
print("Unknown")
}
}
AFNetworkReachabilityManager.sharedManager().startMonitoring()
答案 1 :(得分:1)
您不应该每分钟或定期检查可恢复性。这不是一个好习惯,它会降低应用程序的性能。
您可以获得可更改性更改通知。所以当rechabilty改变时你可以执行一些任务
你可以这样做,
您必须先创建Reachability
对象,然后才能从中接收通知。另外,请务必在您创建的startNotifier()
对象上调用Reachability
方法。这将是一个如何在您的应用程序委托中执行此操作的示例:
class AppDelegate: UIResponder, UIApplicationDelegate
{
private var reachability:Reachability!;
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);
self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();
}
func checkForReachability(notification:NSNotification)
{
// Remove the next two lines of code. You cannot instantiate the object
// you want to receive notifications from inside of the notification
// handler that is meant for the notifications it emits.
//var networkReachability = Reachability.reachabilityForInternetConnection()
//networkReachability.startNotifier()
let networkReachability = notification.object as Reachability;
var remoteHostStatus = networkReachability.currentReachabilityStatus()
if (remoteHostStatus.value == NotReachable.value)
{
println("Not Reachable")
}
else if (remoteHostStatus.value == ReachableViaWiFi.value)
{
println("Reachable via Wifi")
}
else
{
println("Reachable")
}
}
}
下载可访问性类
希望这会有所帮助:)
答案 2 :(得分:0)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// internetReachable is declared as property.
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
// called when network status is changed
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is Connected.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN!");
break;
}
}
//#import "Reachability.m"
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");
Reachability* noteObject = (__bridge Reachability *)info;
// Post a notification to notify the client that the network reachability changed.
[[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}
答案 3 :(得分:0)
您可以使用 - https://github.com/ashleymills/Reachability.swift
//declare this inside of viewWillAppear
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("Unable to create Reachability")
return
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
do{
try reachability?.startNotifier()
}catch{
print("could not start reachability notifier")
}
//declare this inside of viewWillDisappear
reachability!.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self,
name: ReachabilityChangedNotification,
object: reachability)
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
print("NETWORK REACHABLE.")
}
if reachability.isReachableViaWiFi()
{
print("NETWORK REACHABLE VIA WIFI.")
}
if reachability.isReachableViaWWAN()
{
print("NETWORK REACHABLE VIA WWAN.")
}
else {
print("NETWORK NOT REACHABLE.")
}
}
答案 4 :(得分:0)
也适用于我自己的存档目的。您可以在他/她的回答中使用这个作为@Lion。
只需在AppDelegate.swift中的didFinishLaunchingWithOptions中调用 Reachability.registerListener()即可。它会自动通知您更改。
store.subscribe(() => {
const state = store.getState();
// do whatever you want with the new state
});