diff --git a/CHANGELOG.md b/CHANGELOG.md index c98874410..a12aed6b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port +- [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 - [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript - [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override) diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc new file mode 100644 index 000000000..37eb6dcb4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -0,0 +1,46 @@ +Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). + +The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. + + Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). + Failing to do so can drain the battery quickly. + +== Noncompliant Code Example + +[source,swift] +---- +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} +---- + +== Compliant Code Example + +[source,swift] +---- +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} + +func stopMotionUpdates() { + if motionManager.isAccelerometerActive { + motionManager.stopAccelerometerUpdates() + } +} +----