Reputation: 176
I recently was doing a compass, to involve CoreMotion framework, but I checked for a long time did not find information on how to implement this framework the effect of the compass, hoping to get everyone's help.
Upvotes: 0
Views: 775
Reputation: 1
My "Helloworld" CoreMotion:
1) Add CoreMotion.framework
2) in .h file:
#import <CoreMotion/CoreMotion.h>
3) in .m file:
CMMotionManager *myMotionManager= [[CMMotionManager alloc] init];
myMotionManager.deviceMotionUpdateInterval = 1/60;
[myMotionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
double x = myMotionManager.deviceMotion.magneticField.field.x;
double y = myMotionManager.deviceMotion.magneticField.field.y;
double z = myMotionManager.deviceMotion.magneticField.field.z;
NSLog(@"Strength of Earth's magnetic field= %8.2f",sqrt(x*x+y*y+z*z));
myLabel.text = [NSString stringWithFormat:@"%8.2f", sqrt(x*x+y*y+z*z)];
}];
Upvotes: 0
Reputation: 63667
Upvotes: 2