在我的应用程序中,我有一些NSURLConnection.sendAsynchronousRequest,但因为如果用户已禁用wifi,应用程序崩溃.
有没有办法检测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) }