CodexBloom - Programming Q&A Platform

Unexpected Crashes When Using Core Motion in Objective-C on iOS 15

πŸ‘€ Views: 1921 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-21
core-motion ios-15 objective-c Objective-C

I need some guidance on I tried several approaches but none seem to work. I'm wondering if anyone has experience with I've been struggling with this for a few days now and could really use some help. I tried several approaches but none seem to work. I'm experiencing intermittent crashes in my app when I try to access Core Motion data using the `CMMotionManager`. The app works fine on iOS 14, but since updating to iOS 15, I've noticed that it crashes with a EXC_BAD_ACCESS error when starting the motion updates. Here’s a snippet of how I’m implementing it: ```objective-c #import <CoreMotion/CoreMotion.h> @interface ViewController () { CMMotionManager *motionManager; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; motionManager = [[CMMotionManager alloc] init]; if (motionManager.isAccelerometerAvailable) { [self startAccelerometerUpdates]; } } - (void)startAccelerometerUpdates { [motionManager setAccelerometerUpdateInterval:0.1]; [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *data, NSError *error) { if (error) { NSLog(@"Error: %@", error.localizedDescription); return; } NSLog(@"Accelerometer Data: %@", data); }]; } @end ``` I’ve tried wrapping the motion manager initialization and updates in a `@try` block to catch any exceptions, but the app still crashes without hitting my catch block. I also checked if the motion manager was properly initialized before starting the updates. Additionally, I'm using ARC, so I'm not manually managing memory. I’m also seeing the following warning in the console when the crash happens: `-[__NSCFDictionary objectForKey:]: unrecognized selector sent to instance`, which seems to be related to the error handling. Any insights on what might be causing this crash or how to better handle Core Motion updates in iOS 15 would be greatly appreciated! How would you solve this? This is part of a larger service I'm building. Thanks in advance! This is my first time working with Objective-C latest.