Alamofire - detect internet connection
If you're declaring network
inside a function it's probably being deallocated as soon as the scope ends.
Make sure you store it in an instance variable.
Attempt to check internet connection on iOS device with Alamofire
create the common class for check the connectivity
import Foundation
import Alamofire
class Connectivity {
class func isConnectedToInternet() -> Bool {
return NetworkReachabilityManager()!.isReachable
}
}
and call the function where you need
if !Connectivity.isConnectedToInternet() {
// show Alert
return
}
Checking for Internet Connection continually with Alamofire
As Jon Shier and Matt say you shouldn't do this. In fact if you are using Alamofire to download an image I suggest you to using instead AlamofireImage and use this code:
let url = URL(string: yourUrl)!
cell.yourImage.af_setImage(
withURL: url,
placeholderImage: placeholderImage,
imageTransition: .crossDissolve(0.2),
runImageTransitionIfCached: false,
completion: { response in
if response.result.isSuccess {
self.dismissLabel()
} else if response.error?._code == NSURLErrorNotConnectedToInternet{
self.showLabel()
}
})
So basically you can show a label "No internet Connection" when AlamofireImage retrieves a connection error while downloading your image. Instead if it succeeded to download it you dismiss the label.
Related Topics
What Is a "Delegate" in Objective C's iPhone Development
Custom Uitableviewcell Programmatically Using Swift
Enterprise In-House App Distribution
How to Add Strings on X Axis in iOS-Charts
How to Detect Apps First Launch in iOS
In Call Status Bar (Unable to Satisfy Constraints)
Can't Pass Data Correctly to Modal Presentation Using Foreach and Coredata in Swiftui
How to Create a User with Multiple Attributes in Firebase with Swift
Xcode 6 Isn't Autocompleting in Swift
Alternative iOS Layouts for Portrait and Landscape Using Just One .Xib File
Implementing Google Custom Search API in iOS
Why am I Getting Ibtool Failed with Exit Code 255
Is a Date in Same Week, Month, Year of Another Date in Swift
Detecting If iPhone Is in a Dark Room
How to Use Cgaffinetransformmakerotation
Swiftui - How to Initialize an Observedobject Using an Environmentobject as a Parameter