In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of detecting network access in iOS12+". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of detecting network access in iOS12+"?
For many years, Apple's Reachability sample program has been used as the basic code for detecting network access in App. A search of Cocoapods.org will show a long list of third-party libraries, which are basically based on Reachability and take into account issues such as ARC support or Swift compatibility. On WWDC 2018, Apple introduced a new framework in iOS 12: Network.framework, which contains a NWPathMonitor class. This class gives us a way to monitor changes in the state of the network without including third-party libraries or Apple sample code.
Use
Simply import the Network framework and you can use the NWPathMonitor class to create an instance of NWPathMonitor as follows:
Let monitor = NWPathMonitor ()
If you are only interested in changing the state of a particular network adapter, such as Wi-Fi, you can use the init (requiredInterfaceType:) initialization method and provide a NWInterface.InterfaceType value as a parameter to instantiate the NWPathMonitor object to listen for the specified type of network adapter, for example:
Let monitor = NWPathMonitor (requiredInterfaceType: .wifi)
You need to make sure that you keep a reference to the NWPathMonitor object somewhere (for example, using the strong property), otherwise ARC may release the NWPathMonitor object, causing the specified callback to fail to be called.
The types of networks that can be monitored include:
Cellularloopbackother (for virtual or undetermined network types) wifiwiredEthernet
To get notification of a state change, you need to specify a callback for the pathUpdateHandler property that will be called when a state change occurs on the network interface. For example, your mobile network is switching from cellular to Wi-Fi. Then, whenever a state change occurs, an instance of NWPath is returned, which you can use to determine the next action, as shown in the following code:
Monitor.pathUpdateHandler = {path in if path.status = = .satisfied {print ("Connected")}}
The difference between using the no-parameter initialization method and using the initialization method of the specified network adapter is whether the status property of the returned NWPathobject object is satisfied. For example, if you only want to listen to the cellular network and your phone is connected to the Wi-Fi network, the callback method will not be called when the state of the Wi-Fi network changes, and the status of the path will remain unsatisfied because the phone does not use the specified network connection. So, if you just want to know if there is a network connection, whether it's Wi-Fi or cellular, it's best to use a parameterless initialization method.
An interesting problem is that NWPath is part of the Network framework in iOS 12, while it is actually present in iOS 9, but in NetworkExtension.framework, there are some nuances between the two.
You can query the returned NWPath object to see the status information of the device's network adapter. Another more interesting attribute is isExpensive, which identifies whether the data returned by the network interface is expensive, such as using cellular data. We can also query whether DNS, IPv4, or IPv6 is supported. We can call the usesInterfaceType method to see which interface has changed its state and triggered a callback:
Let isCellular: Bool = path.usesInterfaceType (.cellular)
Using NWPathMonitor is a bit like using other iOS API, such as CLLocationManager, where we need to call the start method to start receiving updates, and then call the corresponding stop method when finished. The start method of NWPathMonitor requires that we provide a queue for the object to perform its work:
Let queue = DispatchQueue.global (qos: .background) monitor.start (queue: queue)
When we finish changing the listening state, we just need to call the cancel () method. Note that after we currently call cancel on NWPathMonitor, we cannot start listening again, but we need to instantiate a new NWPathMonitor instance. Note that if you access the currentPath property of NWPathMonitor before calling start (), nil will be returned. In fact, if you print the path that returns to the update callback, it looks like this:
Monitor.pathUpdateHandler = {path in print (path)}
The following will be printed:
Optional (satisfied (Path is satisfied), interface: en0, scoped, ipv4, ipv6, dns)
This indicates that the NWPaths and currentPath properties returned here are optional, although API is not explicitly stated (we can infer that the returned NWPath reference is an Objective-C pointer bridged to Swift).
Captive Portals
Captive Portal is a web page that is displayed during a public Wi-Fi hotspot connection and is usually used to force login, registration, or payment before authorizing access to Internet (or other network resources). In a previous blog post, I mentioned that from the perspective of App development, Reachability seems to be fine, but in fact, because of Captive Portals, it doesn't get the job done. This may cause App not to work properly or even crash-- because App may expect to get some JSON data from RESTful API, but get some HTML from Captive Portals.
I was curious if NWPathMonitor was any better than Reachability in detecting network connections. The NWPath.Status enumeration does provide three cases-- satisfied, unsatisfied, and requiresConnection. Unfortunately, Network.framework 's developer documentation does not provide instructions for using these enumerated values, and if we look at the NetworkExtension.framework documentation, the NWPathStatus object provides satisfiable enumerated values, which contains some relevant documentation descriptions:
The path is not currently satisfied, but may become satisfied upon a connection attempt. This can be due to a service, such as a VPN or a cellular data connection not being activated.
The requiresConnection enumeration value seems to be similar to the satisfiable value of the NWPathStatus object. The good news is that NWPathMonitor usually notifies path that it is set to satisfiable state only after captive portal negotiation, that is, after the web view pops up and the user logs in. In the case of no pop-up captive portal, an Action Sheet is displayed to the user, providing Use Without Internet and Use Other Network options. If the user selects Use Without Internet, the status of the path returned by NWPathMonitor is satisfied, even if it is not actually connected.
Through some experiments with Charles, I found that NWPathMonitor did not report that NWPath's Status was set to statisfied when the Wi-Fi network connection was interrupted while initializing the Wi-Fi network connection unless Use Without Internet was selected. However, if the network connection is restored but subsequently deleted, the change cannot be detected and the state of the path is not still satisfied. This can happen if users pay only one hour for Internet access on the train or hotel.
Connectivity
Connectivity is an MIT-licensed open source framework that aims to reuse iOS's existing methods of detecting captive portal. It allows the use of Reachability on iOS 8 + to accurately detect real Internet connections, which means that we can use this method when NWPathMonitor is not available. And on iOS 12, Connectivity uses NWPathMonitor to provide higher accuracy.
Connectivity already provides support for NWPathMonitor, which can be used in iOS 12 + systems. If the framework property is set to network, the Network framework is used instead of the SystemConfiguration framework (Reachability) to listen for state changes to the network adapter.
Let connectivity = Connectivity () connectivity.framework = .network
After a state change in the network adapter, Connectivity performs a number of checks to determine if Internet access is available. There is also a polling option that can be used to poll whether the network is available, even if the status has not changed. You can do this by setting isPollingEnabled = true and setting pollingInterval to the appropriate time value.
At this point, I believe you have a deeper understanding of "what is the method of detecting network access in iOS12+". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.