Issue Description
Core motion is crashing using iOS 9.1 on device when the user start to move or walk
Steps to reproduce
1- create a sample app alloy project
2. paste the next code:
app/views/index.xml
<Alloy>
<Window backgroundColor="white" layout="vertical">
<Label>Pitch:</Label>
<ProgressBar id="pitch" />
<Label>Roll:</Label>
<ProgressBar id="roll" />
<Label>Yaw:</Label>
<ProgressBar id="yaw" />
</Window>
</Alloy>
app/styles/index.tss
"ProgressBar" : {
"top" : 10,
"width" : 200,
"min" : 0,
"max" : 3.1415927
},
"Label" : {
"font" : {
textStyle: Ti.UI.TEXT_STYLE_SUBHEADLINE
},
"top" : 50,
"left" : 10
}
app/controllers/index.js
var accelX = accelY = accelZ = 0;
var lastX = lastY = lastZ = 0;
var ACCEL_THRESHOLD = 2;
var SHAKE_THRESHOLD = 5;
var CoreMotion = require('ti.coremotion');
if (CoreMotion.isDeviceMotionAvailable()) {
CoreMotion.setDeviceMotionUpdateInterval(500);
var frames = CoreMotion.availableAttitudeReferenceFrames();
var ref_frame = CoreMotion.ATTITUDE_REFERENCE_FRAME_X_TRUE_NORTH_Z_VERTICAL;
if (frames & ref_frame) {
// Use the True North Frame if available
Ti.API.debug('REFERENCE FRAME: True North');
CoreMotion.startDeviceMotionUpdatesUsingReferenceFrame(
{referenceFrame: ref_frame},
updateMotionData
);
}
else if (ref_frame = CoreMotion.getAttitudeReferenceFrame()) {
// Use the default frame if it exists
Ti.API.debug('REFERENCE FRAME: Default ' + ref_frame);
CoreMotion.startDeviceMotionUpdatesUsingReferenceFrame(
{referenceFrame: ref_frame},
updateMotionData
);
} else {
// Do not use a reference frame
Ti.API.debug('REFERENCE FRAME: None');
CoreMotion.startDeviceMotionUpdates(updateMotionData);
}
}
function updateMotionData (e) {
if (e.success) {
var data = e.userAcceleration;
if (Math.abs(lastX - data.x) > ACCEL_THRESHOLD) {
accelX++;
}
if (Math.abs(lastY - data.y) > ACCEL_THRESHOLD) {
accelY++;
}
if (Math.abs(lastZ - data.z) > ACCEL_THRESHOLD) {
accelZ++;
}
analyzeResults();
lastX = data.x;
lastY = data.y;
lastZ = data.z;
data = e.attitude;
$.pitch.message = data.pitch;
$.pitch.value = Math.abs(data.pitch);
$.roll.message = data.roll;
$.roll.value = Math.abs(data.roll);
$.yaw.message = data.yaw;
$.yaw.value = Math.abs(data.yaw);
} else {
if (e.error) Ti.API.error(e.error);
}
}
function analyzeResults(){
if (accelX > SHAKE_THRESHOLD || accelY > SHAKE_THRESHOLD || accelZ > SHAKE_THRESHOLD) {
var err = SHAKE_THRESHOLD * 0.5;
if (accelX > SHAKE_THRESHOLD && (accelY < err && accelZ < err)){
alert("Quit shaking me back and forth!");
}
else if (accelY > SHAKE_THRESHOLD && (accelX < err && accelZ < err)){
alert("Quit shaking me up and down!");
}
else if (accelZ > SHAKE_THRESHOLD && (accelX < err && accelY < err)){
alert("Why are you shaking me like that?!");
}
else {
alert("Quit shaking me!");
}
accelX = accelY = accelZ = 0;
}
}
$.pitch.show();
$.roll.show();
$.yaw.show();
$.index.open();
3- Add the Core Motion Module
4- Build and run on device
5.- Move and Walk
The app will close unexpectedly
Expected Behavior
The app should keep working
**Additional notes:
Attached Xcode crash log image.
This is also happening on the movies sample app
[~rramirez] Do you have infos where it it did work? [~cng] [~penrique] This module is also compiled with the old hyperloop, we should clean it up for now just like in MOD-2150, thoughts?.
This issue was fixed as part of the ti.coremotion refactoring process. The current version can be found [here](https://github.com/appcelerator-modules/ti.coremotion/releases) and will be packed into Ti.SDK 5.2.0 by default. [Docs](https://github.com/appcelerator-modules/ti.coremotion/tree/master/apidoc) and an [example](https://github.com/appcelerator-modules/ti.coremotion/blob/master/iphone/example/app.js) covering every available API is also packed into the module. As we cleaned everything up, the device motion has now its own namespace, the updated sample code following (view and style remain the same):
Thanks!
The module is working great ! Thank you guys!
Closing ticket as fixed. Verified that if I use ti.coremotion (2.0.0), the app will not crash when I move around or leave the app idle. Tested on: Appc CLI NPM: 4.2.2 Appc CLI Core: 5.2.0-229 ti.coremotion: 2.0.0 SDK: 5.2.0.v20160114021251 Node: v4.2.4 OS: El Capitan (10.11.2) Xcode: 7.2 Devices: iphone 6 (9.2)