好吧,所以我对Swift很新,我对我要做的事情有点困惑,或者我是否朝着错误的方向前进.(https://github.com/ashleymills/Reachability.swift)
这是我的viewDidLoad方法:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
checkConnection()
}
Run Code Online (Sandbox Code Playgroud)
然后,我从Reachability GitHub项目中获得了一个带有代码的函数:
func checkConnection() {
//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!
reachability.whenReachable = { reachability in
// this is called on a background thread, but UI updates must
// be on the main thread, like this:
DispatchQueue.main.async {
if reachability.isReachableViaWiFi {
print("Reachable …Run Code Online (Sandbox Code Playgroud) 我想检查设备的互联网连接是否非常慢。
我已经使用Reachability类来检查互联网连接是否可用。
但在这里我想每隔几秒检查一次互联网速度是否不佳。
这是否可以找到,是的,我该怎么做。
我有以下代码来确定是否有互联网连接。这段代码工作正常。我如何知道我是否突然失去连接
var reachability:Reachability?
reachability = Reachability()
self.reachability = Reachability.init()
if((self.reachability!.connection) != .none)
{
print("Internet available")
}
Run Code Online (Sandbox Code Playgroud)
可达性类是否具有报告连接是否断开的功能。如果无法解决可达性问题,其他选择是什么
我Cannot force unwrap value of non-optional type 'Reachability'在行上收到针对iOS 13(Swift 5)的警告xcode 11.1
let reachability = Reachability()!
我尝试了if语句并尝试执行,但是似乎都没有用。给出警告“调用可以抛出,但错误不能从属性初始化器抛出”
import Reachability
class ReachabilityDetect {
let reachability = Reachability()!
var dm = DataModel()
func addObservers(datamodel: DataModel) {
self.dm = datamodel
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
Util.DLog("Reachability notifier niet te starten.")
}
}
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
Util.DLog("WiFi is actief")
self.dm.dataConnectionisWifi = true
self.dm.dataConnectionisCellular = …Run Code Online (Sandbox Code Playgroud)