SoFunction
Updated on 2025-04-13

Summary of the methods of calling various iPhone sensors in iOS App

Use of CoreMotion framework

The CoreMotion framework is very powerful. It not only configures and manages the acceleration sensor and spiral sensor, but also encapsulates many algorithms for us, so we can directly obtain the motion state information of the device.

1. Data processed by CoreMotion

CoreMotion is responsible for processing four types of data, one is acceleration data, one is spiral data, one is magnetic induction data, and the other is the motion data of the device obtained through complex operations of the first three types of data. Several main categories are as follows:

CMAccelerommterData: Device acceleration data

typedef struct {
    double x;
    double y;
    double z;
} CMAcceleration;
@interface CMAccelerometerData : CMLogItem
{
@private
    id _internal;
}
// Acceleration data object
@property(readonly, nonatomic) CMAcceleration acceleration;

@end
CMGyroData: Device spiral data

typedef struct {
    double x;
    double y;
    double z; 
} CMRotationRate;
@interface CMGyroData : CMLogItem
{
@private
    id _internal;
}
//Spiral Data Object
@property(readonly, nonatomic) CMRotationRate rotationRate;

@end
CMMagneometerData: Magnetic induction information

typedef struct {
    double x;
    double y;
    double z;
} CMMagneticField;

@interface CMMagnetometerData : CMLogItem
{
@private
    id _internal;
}

//Magnetic Object
@property(readonly, nonatomic) CMMagneticField magneticField;

@end


CMDeviceMotion: Device motion status data

@interface CMDeviceMotion : CMLogItem
{
@private
    id _internal;
}
//The device's status object
@property(readonly, nonatomic) CMAttitude *attitude;
//The angular velocity of the equipment
@property(readonly, nonatomic) CMRotationRate rotationRate;
//The gravity acceleration of the equipment
@property(readonly, nonatomic) CMAcceleration gravity;
//The acceleration of the user married the device. The total acceleration of the device is the gravity acceleration called the acceleration given by the user.
@property(readonly, nonatomic) CMAcceleration userAcceleration;
//The magnetic field vector object of the device
@property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);
Compared with the previous two classes, this is more complicated. The attitude object encapsulates the state properties of many devices:

@interface CMAttitude : NSObject <NSCopying, NSSecureCoding>
{
@private
    id _internal;
}
//Euler angle roll of the device
@property(readonly, nonatomic) double roll;
//Euler angle pitch of the device
@property(readonly, nonatomic) double pitch;
//Euler angle yaw of the equipment
@property(readonly, nonatomic) double yaw;
//The rotation matrix of device state
@property(readonly, nonatomic) CMRotationMatrix rotationMatrix;
//The quaternion of the device status
@property(readonly, nonatomic) CMQuaternion quaternion;
@end
2. Use of CoreMotion
There are two ways to use CoreMotion. One is to actively ask the manager for data, and the other is to let the manager pass the data to the callback to us through callbacks. These two methods are called pull method and push method respectively.
Pull method:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//Create management object
    manager= [[CMMotionManager alloc]init];
//Open acceleration update
    [manager startAccelerometerUpdates];
//Open the spiral instrument update
    [manager startGyroUpdates];
//Open status update
    [manager startMagnetometerUpdates];
//Create a timer
    NSTimer * time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updata) userInfo:nil repeats:YES];
    = [NSDate distantPast];
}

-(void)updata{
//Get data
    NSLog(@"%f,%f,%f\n%f,%f,%f",,,,,,);
  
   
}
Push method:

//Create management object
    manager= [[CMMotionManager alloc]init];
//Callback in the current thread
    [manager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
         NSLog(@"%f,%f,%f\n%f,%f,%f",,,,,,);
    }];
3. More properties and methods of CoreMotion
@interface CMMotionManager : NSObject
{
@private
    id _internal;
}
//Set the acceleration sensor to update the frame rate
@property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval __TVOS_PROHIBITED;
//Is the acceleration sensor available
@property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable __TVOS_PROHIBITED;
//Is the acceleration sensor activated
@property(readonly, nonatomic, getter=isAccelerometerActive) BOOL accelerometerActive __TVOS_PROHIBITED;
// Acceleration sensor data object
@property(readonly, nullable) CMAccelerometerData *accelerometerData __TVOS_PROHIBITED;
//Pull method starts updating acceleration data
- (void)startAccelerometerUpdates __TVOS_PROHIBITED;
//Push method to update acceleration data
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler __TVOS_PROHIBITED;
//Stop updating acceleration data
- (void)stopAccelerometerUpdates __TVOS_PROHIBITED;
//Spiral sensor refresh frame rate
@property(assign, nonatomic) NSTimeInterval gyroUpdateInterval __TVOS_PROHIBITED;
//Is the spiral instrument available
@property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable __TVOS_PROHIBITED;
//Is the spiral activated
@property(readonly, nonatomic, getter=isGyroActive) BOOL gyroActive __TVOS_PROHIBITED;
//Spiral meter data
@property(readonly, nullable) CMGyroData *gyroData __TVOS_PROHIBITED;
//Pull mode starts to update the spiral instrument
- (void)startGyroUpdates __TVOS_PROHIBITED;
//Push method starts to update the spiral instrument
- (void)startGyroUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler __TVOS_PROHIBITED;
//Stop updating the spiral
- (void)stopGyroUpdates __TVOS_PROHIBITED;
//Magnetic sensing update frame rate
@property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Is the device magnetic sensor available
@property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Is the device magnetic sensor activated
@property(readonly, nonatomic, getter=isMagnetometerActive) BOOL magnetometerActive NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
// Equipment magnetic state data
@property(readonly, nullable) CMMagnetometerData *magnetometerData NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Pull method to update the device's magnetic state
- (void)startMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Push method to update the device's magnetic state
- (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Stop updating device status
- (void)stopMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Update status update frame rate
@property(assign, nonatomic) NSTimeInterval deviceMotionUpdateInterval __TVOS_PROHIBITED;
//Reference device enumeration
+ (CMAttitudeReferenceFrame)availableAttitudeReferenceFrames NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
@property(readonly, nonatomic) CMAttitudeReferenceFrame attitudeReferenceFrame NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Is the equipment motion information available
@property(readonly, nonatomic, getter=isDeviceMotionAvailable) BOOL deviceMotionAvailable __TVOS_PROHIBITED;
//Is the device motion information activated
@property(readonly, nonatomic, getter=isDeviceMotionActive) BOOL deviceMotionActive __TVOS_PROHIBITED;
//Equipment motion information object
@property(readonly, nullable) CMDeviceMotion *deviceMotion __TVOS_PROHIBITED;
//Pull mode starts refreshing the motion information
- (void)startDeviceMotionUpdates __TVOS_PROHIBITED;
//Push method starts refreshing the motion information
- (void)startDeviceMotionUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler __TVOS_PROHIBITED;
//Use a reference system
- (void)startDeviceMotionUpdatesUsingReferenceFrame:(CMAttitudeReferenceFrame)referenceFrame NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Push method starts refreshing device motion information
- (void)startDeviceMotionUpdatesUsingReferenceFrame:(CMAttitudeReferenceFrame)referenceFrame toQueue:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//Stop refreshing device motion information
- (void)stopDeviceMotionUpdates __TVOS_PROHIBITED;

Application of distance sensors
The iPhone has a distance sensor built-in, which is located near the handset of the phone. When we get close to the handset when we are making a phone call, the screen of the phone will automatically turn off, which is controlled by the distance sensor.
When we develop an app, we can also call some interface methods of the distance sensor if needed. The interface of the distance sensor is very simple, and the distance changes are notified mainly through the notification center.
First, we need to enable the distance sensor application:

[UIDevice currentDevice].proximityMonitoringEnabled=YES;
Notice of listening distance change:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice) name:UIDeviceProximityStateDidChangeNotification object:nil];
In the callback method, we can listen to the distance state through the following attribute:

-(void)notice{
    if ([UIDevice currentDevice].proximityState) {
NSLog(@"close");
    }else{
NSLog(@"Long Distance");
    }
}