Overview sensor
The iphone comes with many sensors, which are common in the following table:
The system is used for a variety of sensors, eg: distance sensor use
//Open Range Sensing function[Uidevice currentdevice].proximitymonitoringenabled =YES;//Notification of listening distance sensing[[Nsnotificationcenter Defaultcenter] Addobserver:selfselector: @selector (proximitychange:) Name: UideviceproximitystatedidchangenotificationObject: nil];- (void) Proximitychange: (Nsnotificationcenter *) Notification {if([uidevice Currentdevice].proximitystate = =YES) {NSLog (@"an object is close to the device screen");//The screen will automatically lock}Else{NSLog (@"an object is away from the device screen");//The screen will automatically unlock }}
More device information
Device model, CPU status, memory usage, hard disk usage ...
Whether to jailbreak, what sensors are installed, the processes currently running ...
There are 2 ways to get more device information
Import the underlying C-language library and get it through the underlying C-language function (more complex, which takes a lot of time to study)
Use third-party libraries (the underlying C functions are encapsulated with OC)
Https://github.com/Shmoopi/iOS-System-Services
Https://github.com/erica/uidevice-extension
Acceleration meter
The accelerometer can be used to detect the movement of a device, and common applications such as shake/pedometer are based on accelerometers.
"Accelerometer mainly through: testing equipment in the X, Y, Z axis acceleration (which direction of the powerful role, which direction of movement), according to the acceleration value, you can determine the role of the force in all directions
The development of accelerometers is mainly in two ways:
Using uiaccelerometer, the usage is very simple (the iOS5 has expired)
Starting from iOS4:coremotion. Framework
Uiaccelerometer Development Steps
1. Get Singleton objects
Uiaccelerometer *accelerometer = [Uiaccelerometer sharedaccelerometer];
2, set up the agent
Accelerometer. delegate = self;
3. Set Sampling interval
1.0/30.0// 1 seconds sampling 30 times
4. Implement Agent Method
-(void) Accelerometer: (Uiaccelerometer *) Accelerometer didaccelerate: (uiacceleration *) acceleration The x, Y, z three properties in//acceleration represent the acceleration on each axis, respectively
Coremotion (Core Service Layer)
The accelerometer has been fully upgraded in IOS4, introducing a gyroscope and adding a framework specifically for motion Coremotion.framework
Core Motion not only provides real-time acceleration values and rotational speed values, but more importantly, Apple integrates a lot of awesome algorithms into it.
"Coremotion two ways to get data
Push: Capture all data in real-time (high acquisition frequency)-the same way as Uiaccelerometer
Pull: Take the initiative to collect data when you need it
"Push Development steps
//1. Create a coremotion manager//Cmmotionmanager *mgr = [[Cmmotionmanager alloc] init];Self.mgr =[[Cmmotionmanager alloc] init]; //2. Determine if the accelerometer is available if(self.mgr.isAccelerometerAvailable) {/*is isaccelerometeractive collecting data from Accelerometerdata startaccelerometerupdates pull Startaccelerometerupdatestoqueue Push stopaccelerometerupdates Stop acquisition accelerometerupdateinterval sampling time */ //3. Set the sampling timeSelf.mgr.accelerometerUpdateInterval =1/30.0; //4. Start sampling[Self.mgr startaccelerometerupdatestoqueue:[nsoperationqueue mainqueue] Withhandler:^ (Cmaccelerometerdata *accelerometerdata, Nserror *error) { //This block is called when data is collected . if(Error)return ; Cmacceleration Acceleration=accelerometerdata.acceleration; NSLog (@"x =%f y =%f z =%f", acceleration.x, ACCELERATION.Y, acceleration.z); }]; }Else{NSLog (@"Accelerometer Not available"); }
"Pull Development steps
// Create a Motion manager Object Cmmotionmanager *mgr = [[Cmmotionmanager alloc] init]; // determine if the accelerometer is available if // Accelerometer Available} // Start sampling -(void) startaccelerometerupdates; // acquisition of acceleration data when needed Cmacceleration acc = mgr.accelerometerData.acceleration; NSLog (@ "%f,%f,%f", acc.x, Acc.y, acc.z);
Shake a Shake
A way to monitor the shaking
Method 1: Analyze the accelerometer data to determine if a shake operation is performed (more complex)
Method 2:ios comes with the Shake Monitoring API (very simple)
To judge the shaking of the steps: the implementation of 3 shaking a shake monitoring method
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *)event/*/ -(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *)event/* */-(void) motionended: (uieventsubtype) Motion withevent: (uievent *)event/ **/
Sensor + Accelerometer + Shake shake