异步 – 检测是否在swift中启用了wifi
发布时间:2020-12-14 04:25:12 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我有一些NSURLConnection.sendAsynchronousRequest,但因为如果用户已禁用wifi,应用程序崩溃. 有没有办法检测wifi是否被禁用,所以我可以这样做: if(wifi is enabled){ NSURLConnection.sendAsynchronousRequest} 解决方法 Found the answer
在我的应用程序中,我有一些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) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |