in my app i am checking that if mobile data is off its showing the popup like check your data connection. for that i write this code
import Foundation
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .Reachable
let needsConnection = flags == .ConnectionRequired
return isReachable && !needsConnection
}
}
but by this code its only check that wifi is connected or not. but if i try with 3g mobile data its always showing me that your mobile data is not connected. so how can i solve this?
If you have any super Class in your application then use below code
Here's how I do it and it works for me. In my viewDidLoad:
And the reachabilityChanged
Using this Reachability
Try like below code:
Create object of reachability class like,
Now write below code in viewDidLoad(),
Now create function to check reachabilty of wifi as well as data pack,
Hope this will help you.