Open hgroschaus opened 1 year ago
OS | OS version | Langage |
---|---|---|
IOS | - | Swift |
ID | Title | Category | Sub-category |
---|---|---|---|
- | Network should check Signal Condition | - | - |
Severity | Remediation Cost |
---|---|
Minor | Minor |
Don’t attempt to perform network operations when the network is unavailable.
If network operations fail, use the SCNetworkReachability
API to to see whether the host is available.
If there are signal problems, alert the user or defer work until the host is available again.
To determine whether a host is reachable, check for the absence of the kSCNetworkReachabilityFlagsReachable
reachability flag
import SystemConfiguration
let hostName = "exampleHostName"
let reachability = SCNetworkReachabilityCreateWithName(nil, (hostName as NSString).UTF8String).takeRetainedValue()
// Create a place in memory for reachability flags
var flags: SCNetworkReachabilityFlags = 0
// Check the reachability of the host
SCNetworkReachabilityGetFlags(reachability, &flags)
// Check to see if the reachable flag is set
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
. . .
}
<p>Using <code>SCNetworkReachabilityGetFlags</code> of <code>module SystemConfiguration</code> to check the signal condition</p>
<p>Perform an error handling</code> which will prevent any unnecessaries operations</p>
<h2>Noncompliant Code Example</h2>
<pre>
import SystemConfiguration
let hostName = "exampleHostName"
let reachability = SCNetworkReachabilityCreateWithName(nil, (hostName as NSString).UTF8String).takeRetainedValue()
// Create a place in memory for reachability flags
var flags: SCNetworkReachabilityFlags = 0
// Check the reachability of the host
SCNetworkReachabilityGetFlags(reachability, &flags)
// Check to see if the reachable flag is set
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
. . .
}
</pre>
SCNetworkReachabilityCreateWithName
method.SCNetworkReachabilityGetFlags
methodSCNetworkReachabilityFlags
and kSCNetworkReachabilityFlagsReachable
Don’t attempt to perform network operations when the network is unavailable.
If network operations fail, use the
SCNetworkReachability
API to to see whether the host is available. If there are signal problems, alert the user or defer work until the host is available again.To determine whether a host is reachable, check for the absence of the
kSCNetworkReachabilityFlagsReachable
reachability flag