I am new to ios development, i have to check the internet network based on that i will show the view controllers, i used this code for test the network.
class Connectivity {
class func isConnectedToInternet() ->Bool {
return NetworkReachabilityManager()!.isReachable
}
}
if Connectivity.isConnectedToInternet() {
print("Yes! internet is available.")
// do some tasks..
}
else {
}
with this code i am getting network network status, my aim is when the application launch time if don't have network show the default page, if enable the network automatically it will show the main page please help me.
try this code
let reachability = Reachability()!
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
#objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .none:
print("Network not reachable")
}
}
Related
I need an event or like an observer while switching between wifi networks or wifi to cellular and vice versa. Can anybody help me with it? I am uploading a video file and while uploading I am switching between wifi networks or auto-switch between cellular to wifi. So need an event for the same.
It is included in the example on the Reachability github page
//declare this property where it won't go out of scope relative to your listener
let reachability = try! Reachability()
//declare this inside of viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
And the method
#objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .unavailable:
print("Network not reachable")
}
}
To check whether the device is connected or not, you can use NWPathMonitor, like this:
import Network
struct ConnectivityMonitor {
static let monitor = NWPathMonitor()
static func setup() {
ConnectivityMonitor.monitor.pathUpdateHandler = { path in
//path.isExpensive tells you whether the user is using cellular data as opposed to wifi. "true" if cellular data.
if path.status == .satisfied {
//Connected
} else {
//Not connected
}
}
let queue = DispatchQueue(label: "Monitor")
ConnectivityMonitor.monitor.start(queue: queue)
}
}
I am working on iOS application. I have to call api while user online and offline.
I have viewcontroller 1 class and there I am calling api.
override func viewDidLoad() {
super.viewDidLoad()
if let reachability = manager.sharedInstance.internetReachability {
NotificationCenter.default.addObserver( self, selector: #selector( self.reachabilityChanged),name: Notification.Name.reachabilityChanged, object: reachability )
}
}
#IBAction func callAPI(_ sender: Any) {
self.callApi()
}
func callApi() {
//call api
}
#objc private func reachabilityChanged( notification: NSNotification ) {
let reachability = notification.object as! Reachability
switch reachability.connection {
case .wifi:
self.callApi()
case .cellular:
self.callApi()
case .none:
case .unavailable:
}
}
The issue is here if I am in viewcontroller 2 class and app comes to online, The api is not getting trigger in Viewcontroller 1.
The requirement is, If user is offline and comes to online, user is in viewcontroller 2, I have to call that api in viewcontroller 1 without any action, Just by tracking internet is active and I have to call API.
Any suggestions?
The problem is if there is no instance of the view controller 1 existed at the time of connectivity changes, you have no chance to call the API.
If you have no reason having to keep the calling API inside the view controller 1 when connectivity changes, I suggest moving the observer to somewhere that can alive throughout your app's lifetime. The simplest place is in AppDelegate.
What I did,
if no internet then store data in database
make a function in appdelegate e.g callApi()
when app comes to online (internet on) reachabilityChanged( notification: NSNotification ) call, So my code looks like
#objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
appDel.UploadUserRecord() //callApi()
case .cellular:
print("Reachable via Cellular")
appDel.UploadUserRecord() //callApi()
case .unavailable:
print("Network not reachable")
case .none:
print("Network none")
}
}
I'm Used Reachability class globally
I have written code to check the status of a network connection. I used the Reachability library.
let reachability = Reachability()!
reachability.whenReachable = { reachability in
if reachability.connection == .wifi {
print("Reachable via WiFi")
CustomActivityIndicator.shared.hide(uiView: self.view)
} else {
print("Reachable via Cellular")
CustomActivityIndicator.shared.hide(uiView: self.view)
}
}
reachability.whenUnreachable = { _ in
print("Not reachable")
CustomActivityIndicator.shared.show(uiView: self.view, labelText: "Not reachable", backgroundColor: self.color, textColor: .white, animated: false, duration: 0)
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
I also wrote code that notifies the user via the CustomActivityIndicator when the network is disconnected and hides the CustomActivityIndicator when reconnected.
Where should I put the code so that it works on all screens of the app?
The best approach is: create a base view controller and inherit all your view controllers from the BaseViewController and wrote the common code i.e Reachability in this context in that base class.
I've included my code below. If there's no wifi and no cellular service, the app freezes. If I turn cellular off altogether, it prints ""Not reachable", which is expected, and the app works fine. But if there's no service and cellular is on, (for instance, in the subway), it says "reachable via cellular" and I can't interact with the app. It's just frozen, even though that code is on the background thread.
let reachability = Reachability()
#IBAction func rateButtonAction(_ sender: Any) {
if #available(iOS 10.3, *) {
DispatchQueue.global(qos: .background).async {
if self.reachability?.connection == .wifi {
print("Reachable via WiFi")
SKStoreReviewController.requestReview()
} else if self.reachability?.connection == .cellular {
print("Reachable via Cellular")
SKStoreReviewController.requestReview()
} else if self.reachability?.connection == .none {
print("Not reachable")
} else {
print("Not reachable")
}
}
} else {
print("Rate didn't work")
}
}
I used Reachability library for checking that th iPhone reachs to the internet. If the app is running, It receive fine the notification. My question is how to make the app receive the notification when It isn't running?
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AutoCheckin.reachabilityChanged(_:)),name: ReachabilityChangedNotification,object: reachability)
do{
try reachability?.startNotifier()
}catch{
print("could not start reachability notifier")
}
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
if reachability.isReachableViaWiFi() {
logMessage("Reachable via WiFi: ")
print("Reachable via WiFi: ")
} else {
logMessage("Reachable via Cellular: ")
print("Reachable via Cellular")
}
self.pushLocalNotification()
}else{
logMessage("Network not reachable")
print("Network not reachable")
}
}
And also I need to send a local notification if there is an internet.