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.
+import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} ++
+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() + } +} +\ No newline at end of file diff --git a/swift-lang/src/main/resources/io/ecocode/rules/swift/EC514.json b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC514.json new file mode 100644 index 0000000..481f681 --- /dev/null +++ b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC514.json @@ -0,0 +1,18 @@ +{ + "key": "EC514", + "title": "Motion Sensor Leakage", + "defaultSeverity": "Major", + "description": "Any motion sensor started should be stopped.", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "5min" + }, + "tags": [ + "ecocode", + "environment", + "sobriety", + "eco-design" + ], + "type": "CODE_SMELL" +} \ No newline at end of file diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java index db5c5f3..6d51e57 100644 --- a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java @@ -54,8 +54,8 @@ public void testMetadata() { } @Test - public void testRegistredRules() { - assertThat(repository.rules()).hasSize(12); + public void testRegisteredRules() { + assertThat(repository.rules()).hasSize(13); } @Test diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/MotionSensorLeakCheckTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/MotionSensorLeakCheckTest.java new file mode 100644 index 0000000..dc3568e --- /dev/null +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/MotionSensorLeakCheckTest.java @@ -0,0 +1,51 @@ +/* + * ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications + * Copyright © 2023 green-code-initiative (https://www.ecocode.io/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see