green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[iOS] Network should check Signal Condition #11

Open hgroschaus opened 1 year ago

hgroschaus commented 1 year ago

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) {
    . . .
}
hgroschaus commented 1 year ago

Optimized API: List Shallow Copy - Module copy

Platform

OS OS version Langage
IOS - Swift

Main caracteristics

ID Title Category Sub-category
- Network should check Signal Condition - -

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Don’t attempt to perform network operations when the network is unavailable.

Rule complete description

Text

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) {
    . . .
}

HTML

<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>

Implementation principle