如果WiFi在迅速启用检测 [英] Detecting if the wifi is enabled in swift
本文介绍了如果WiFi在迅速启用检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序我有一些NSURLConnection.sendAsynchronousRequest而是因为,如果用户已禁用无线应用程序崩溃。
有没有一种方法,如果禁用了WiFi,来检测这样我就可以做这样的事情:
如果(WiFi是启用)
{
NSURLConnection.sendAsynchronousRequest
}
解决方案
第1步:
添加SystemConfiguration框架到您的项目
第二步:
这个功能添加到您的项目:
FUNC isConnectionAvailble() - GT;布尔{ 变种rechability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefaultwww.apple.com)。takeRetainedValue() VAR标志:SCNetworkReachabilityFlags = 0 如果SCNetworkReachabilityGetFlags(rechability,&安培;标志)== 0
{
返回false
} 让isReachable =(旗&安培; UInt32的(kSCNetworkFlagsReachable))!= 0
让needsConnection =(旗&安培; UInt32的(kSCNetworkFlagsConnectionRequired))!= 0
回报(isReachable&安培;&安培;!needsConnection)
}
In my app i have some NSURLConnection.sendAsynchronousRequest but because of that if the user has wifi disabled the app crashes.
Is there a way to detect if the wifi is disabled so i can do something like that:
if(wifi is enabled)
{
NSURLConnection.sendAsynchronousRequest
}
解决方案
Found the answer in this blog post:
Step 1:
Add "SystemConfiguration" framework to your project
Step 2:
Add this function to your project:
func isConnectionAvailble()->Bool{
var rechability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.apple.com").takeRetainedValue()
var flags : SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(rechability, &flags) == 0
{
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
这篇关于如果WiFi在迅速启用检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文