About Alamofire -- NetworkReachabilityManager listening network status, I have the following two questions.
1.I need to start listening to the network status in which method?
2.If I want to network status monitoring in - application: didFinishLaunchingWithOptions: method, whether to need to delay?
I hope you can help me?
There is a custom class called Reachability (https://github.com/tonymillion/Reachability)
It works with Grand Central Dispatch so it handles the notifications when connectivity is reached or lost, the author warns you about changing it's name because some apps have been reported as denied during the App Store review process because the SDK has already defined a class with the same name
Related
I am saving user entered data to MySQL Database using urlSession’s dataTask when network is available. I am saving user entered data in local SQLite when no network available.
How to schedule background task for syncing it when network is available even when app is not running?
You cannot check the connectivity status and make the URL Request if your app is not running. An alternate solution may be to make the request once the app is opened by the user the next time. At that time you will get a call back in func applicationDidFinishLaunching in the AppDelegate. At this point you check if you have data which you need to sync to the MySql Data base and make the request.
Edit
As you mentioned in the comment. In iOS you cannot detect the network when the app is closed SO Post. It is possible to do so using backgroundMode. Refer to this SO Post
But as you pointed. There is one way of achieving it by geo fencing. An iOS Application wakes up when it enters a region you set a geo fence to. So at that you can make the API request to wake the app. SO Post 2
Edit2
You can wake up the app using silent notification.
Hope this helps. Please let me know if you need any clarifications. Welcome to stackoverflow. Please mark the answer if it you solves the issue you are facing.
I noticed by in Boingo's Wi-Finder iOS app, that the app pushes a notification to the user once he reaches an access point regestered at Boingo's database. The notification tells the user that he can access the wifi network.
How is that done iOS?
I think it can be done by observing the device's connectivity with Reachability class in AppDelegate 's didFinishLaunchingWithOptions
and when networkStatus is equals to ReachableViaWiFi then call application.registerForRemoteNotifications()
Update :
there are 2 types of notifications : Remote and Local
you can configure your app to send local notification but you need to set it contents before,
here you can find Apple's guide on how to handle local notification
You can make use of CLLocationManager and UserNotifications to achieve this.
You can use the region-monitoring services like CLCircularRegion class (to get geographical regions) or CLBeaconRegion class (to get beacon regions) to get notified when the user crosses a region-based boundary. If the boundary crossing occurs even if your app isn't running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event.
In this case, the options dictionary passed to the application(_:didFinishLaunchingWithOptions:) method of your app delegate contains UIApplicationLaunchOptionsKey.location. Then trigger a local notification in order to notify the user the available services in that location.
Examples:-
Using location monitoring:- https://www.raywenderlich.com/136165/core-location-geofencing-tutorial
Using iBeacons - https://www.raywenderlich.com/101891/ibeacons-tutorial-ios-swift
NB. As suggested in other answers Reachability class also we can use. But Reachability cannot be used if the app is in background or terminated. To trigger reachability when app is in background we can follow this: https://stackoverflow.com/a/35615381/4637057. But unfortunately, the disadvantage here is, the more often the Background Fetch is set to work to an app, the more resources will be used. iOS protects itself and the device by applying limitations to the apps that try to use the API very often, so be cautious when setting custom time intervals. Also Background Fetch will cause a battery drain very quickly.
Also only if the app get connected to that wifi network, Reachability can trigger notification. But Boingo will notify the user even if the app is in background and phone is not yet connected to the wifi. So I strongly suspect they are using location services to detect whether user entered an area where wifi is available.
You can get the SSID of the Wifi hotspot and if it's registered one, you can follow Mohy's answers.
Please check the following link.
How to get Wifi SSID in iOS9 after CaptiveNetwork is deprecated and calls for Wifi name are already blocked
This will help you to get the SSID of Wifi hotspot.
I want to block my app's ui when there is no network connectivity.
I saw this post about checking network connectivity.
Is there a way to register for a listener with callback "onNetworkLost()", "onNetworkAvailable()"? Instead of creating my own periodic task?
You can use Reachability class to get notification when network status has changed. Refer https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html for documentation as well as sample code on integrating Reachability. It gives you kReachabilityChangedNotification notification which is broadcasted when network status has changed. Once you get the notification you can check whether network is available or not.
You can also refer this How to use reachability class to detect valid internet connection?
I'm new to developing on iOS and in Objective-C.
According to this technical note (https://developer.apple.com/library/ios/qa/qa1693/_index.html),
It is recommended to check for Reachability using async APIs on app startup, in order to prevent the app from being killed by the Watchdog.
I have looked at the samples + documentation here: SCNetworkReachabilitySetCallback
This allows registering a callback that is asynchronously invoked when reachability state changes.
However, this does not solve the issue of detecting what the state is RIGHT NOW (the callback is only raised on changes to reachability state).
What is the preferred way of getting the reachability state flags ? is there an Async API for that ?
I would look at this tutorial which uses the Tony Million Reachability class.
I have read in the App Store Submission Tips that
If your application provides functionality that requires access to a network, it's very important that your code include a customer alert or notification when the network is not available.
In fact, there are two entries in that submission tips list concerning Reachability (Don't Forget to Include Network Error Alerts in Your Code and Be Sure to Provide Network Error Messages). But I don´t know how an app is expected to manage Reachability actually:
1) Should you listen for network reachability status changes, and notify the user every time the network is not available? Or should you check for the reachability of the network when you are about to perform a network operation, and then notify if needed? Or both?
2) Is it required to check for the reachability of the certain remote hosts you need to call in your network operations, or checking for network availability (either WiFi or WWAN) will be enough?
I'd appreciate some guidance from someone who had already successfully submitted an app to the App Store.
Thanks in advance
1) If your app only needs to access the network when the user specifically chooses to do something, then checking at that time is fine. Depending on your app, you might want to listen for changes in reachability and update your UI based on the current status (such as disable a button if there is no network connection). Don't pop alerts every time the reachability status changes. That would be annoying.
2) Depends on your needs. If you have something that always connects to a specific host then checking that host would be good. If the access can be to anything on the Internet then simply check for Internet access.
All of this can be done with the Reachability class from the "Reachability" sample app.