在我的应用程序中,我有一些NSURLConnection.sendAsynchronousRequest,但因为如果用户已禁用wifi,应用程序崩溃. 有没有办法检测wifi是否被禁用,所以我可以这样做: if(wifi is enabled){ NSURLConnection.se
有没有办法检测wifi是否被禁用,所以我可以这样做:
if(wifi is enabled)
{
NSURLConnection.sendAsynchronousRequest
}
Found the answer in this blog post:
步骤1:
将“SystemConfiguration”框架添加到项目中
第2步:
将此功能添加到您的项目:
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)
}
