Accelerometer works when for iOS application

I use the accelerometer in iOS and I want it to work in the background.

I tried using CMMotionManager like this, but this did not work:

 CMMotionManager*manager= [[CMMotionManager alloc] init]; if(!manager.accelerometerAvailable) { NSLog(@"Accelerometer not available"); } else { manager.accelerometerUpdateInterval = 0.1; NSOperationQueue *motionQueue = [[NSOperationQueue alloc] init]; [manager startAccelerometerUpdatesToQueue: motionQueue withHandler: ^(CMAccelerometerData *data, NSError *error) { NSLog(@"Accelerometer data: %@", [data description]); } ]; } 

How can i do this?

+4
source share
1 answer

Not sure if this solves your problem, but a look at the docs ( http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html ) says:

"Performing lengthy background tasks

For tasks that require longer execution time for implementation, you must request specific permissions to run them in the background without pausing them. On iOS, only certain types of applications are allowed to run in the background:

  • Applications that play audio content for the user in the background, for example, an application for a music player

  • Applications that keep users informed of their location, such as a navigation application

  • Voice over Internet Protocol (VoIP) Applications

  • Press applications that need to download and process new content

  • Applications regularly receiving updates from external accessories

Applications that implement these services must advertise the services they support and use the system framework to implement relevant aspects of these services. Declaring services allows the system to know which services you are using, but in some cases it’s a system framework that actually prevents your application from pausing. "

I'm not sure if you are trying to use the accelerometer to perform any of these actions (perhaps to communicate with the device with a blue tooth?), But if so, you need to declare the services that you support in the application. To do this, you need to add the UIBackgroundModes key to your Info.plist, and then add an array containing the corresponding row (s) for the services you are trying to use. See the Document Page above for a complete list of lines.

If you're not trying to use the accelerometer to do any of these things, it looks like you might be out of luck. Although I would like to be wrong there .. (anyone?)

+1
source

Source: https://habr.com/ru/post/1437753/


All Articles