|
Hey guy. I am having an issue with that function that I found on hackingwithswift.com
when I turn the wifi off, it always return "internet" Then when I turn it on, it prints "no internet" and then a second after "internet". What am I doing wrong? Or is there any better way to check the internet connection? I don't necessarily need to check it everytime the network status changes. Only when some button is tapped. Many thanks. |
|
Hi, well this is quite a complicated topic for a few reasons. The behavior looks like you have Cellular data, and when you switch back to WiFi there is a small window when the internet does not work because it is switching connections... As for checking if there is connection before doing something, the problem is that there might be but then disappear once you start the main work. I think there is even mention in the official Apple guidelines to not check for connection before doing other network related tasks. I think better idea is to just try it and report to user possible problems. Imagine you implement checking network connection against some popular DNS, but maybe DNS will go down and user cannot continue, even though they have connection and your API is also working fine.
If you really want some checking, I would keep using the |
|
Hey Filip! I am currently using Firebase in my project. The issue is (altough it's a good thing if you think about it) that Firebase is smart enough to not return an error when there is no connection but to wait with the execution until the connection appears. Therefore I cant return any error. While potential user may search through something that already does not exist in a firebase while the network is off... |
|
Use this instead, it will fix your issue... if path.status != .unsatisfied { print("internet") } |
|
|
|
The way I did it
This straight from my project |
|
@NigelGee. that looks great! I am super new to Swift and come from a C# background... so I take it Task is basically a threading method or library? At this time I do not have an app in mind just yet, but I did a lot of middleware coding for a larger .net app and I always was dealing with network related routines, many that needed threading .... thank you so much!!!! |
|
|
|
@NigelGee, with the post at the top of the page, how best would Task be used? I tried something like
|
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.