Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6998409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:24:29+00:00 2026-05-27T20:24:29+00:00

i am trying to initialize the device motion manager in a class that contains

  • 0

i am trying to initialize the device motion manager in a class that contains all of my global variables to use in different classes. However i cant seem to get this working.

I have this in my global class:

// Motion Manager
CMMotionManager *motionManager;

Then set the property in a different class header file:

@property (retain) CMMotionManager *motionManager;

And on the .m file i synthetize and start the updates:

@synthesize motionManager;

motionManager = [[CMMotionManager alloc] init];

if (motionManager.deviceMotionAvailable) 
    {
        motionManager.deviceMotionUpdateInterval = 1.0/100.0;
        [motionManager startDeviceMotionUpdates];
        NSLog(@"Device Started");

    }

But when i call this on my third class:

motionManager.deviceMotionAvailable

It returns NO.

PD: both classes import the global class, and the third class imports the second one’s header.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:24:30+00:00Added an answer on May 27, 2026 at 8:24 pm

    Ok it Seems i can use a singleton class to achieve this:

    h file:

    //
    //  MotionManagerSingleton.h
    //  Air Interface
    //
    //  Created by MacBook on 11/12/28.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //  File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
    //  More information about this template on the post http://mk.sg/89
    //  Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
    
    #import <Foundation/Foundation.h>
    #import <CoreMotion/CoreMotion.h>
    
    @interface MotionManagerSingleton : NSObject
    
    + (MotionManagerSingleton*) sharedInstance;
    
    - (void)startMotionManager;
    - (CMAcceleration)getCurrentAcceleration;
    - (CMAttitude*)getCurrentAttitude;
    - (CMAttitude*)getStartingAttitude;
    - (void)setStartingAttitude: (CMAttitude*) ref;
    - (bool)isDeviceReady;
    - (void)destroyMotionManager;
    - (NSTimeInterval) getStamp;
    
    
    
    @end
    

    m file:

    //
    //  MotionManagerSingleton.m
    //  Air Interface
    //
    //  Created by MacBook on 11/12/28.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //  File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
    //  More information about this template on the post http://mk.sg/89    
    //  Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
    
    #import "MotionManagerSingleton.h"
    
    
    
    @implementation MotionManagerSingleton
    
    CMMotionManager *motionManager;
    CMAttitude *referenceAttitude;
    bool initialized;
    
    #pragma mark -
    #pragma mark Singleton Methods
    
    + (MotionManagerSingleton*)sharedInstance {
    
        static MotionManagerSingleton *_sharedInstance;
        if(!_sharedInstance) {
            static dispatch_once_t oncePredicate;
            dispatch_once(&oncePredicate, ^{
                _sharedInstance = [[super allocWithZone:nil] init];
                });
            }
    
            return _sharedInstance;
    }
    
    + (id)allocWithZone:(NSZone *)zone {    
    
        return [self sharedInstance];
    }
    
    
    - (id)copyWithZone:(NSZone *)zone {
        return self;    
    }
    
    #if (!__has_feature(objc_arc))
    
    - (id)retain {  
    
        return self;    
    }
    
    - (unsigned)retainCount {
        return UINT_MAX;  //denotes an object that cannot be released
    }
    
    - (void)release {
        //do nothing
    }
    
    - (id)autorelease {
    
        return self;    
    }
    #endif
    
    #pragma mark -
    #pragma mark Custom Methods
    
    // Add your custom methods here
    - (void)startMotionManager{
        if (!initialized) {
            motionManager = [[CMMotionManager alloc] init];
            if (motionManager.deviceMotionAvailable) {
                motionManager.deviceMotionUpdateInterval = 1.0/70.0;
                [motionManager startDeviceMotionUpdates];
                NSLog(@"Device Motion Manager Started");
                initialized = YES;
            }
        }
    }
    - (CMAcceleration)getCurrentAcceleration{
        return motionManager.deviceMotion.userAcceleration;
    }
    - (CMAttitude*)getCurrentAttitude{
        return motionManager.deviceMotion.attitude;
    }
    - (CMAttitude*)getStartingAttitude{
        return referenceAttitude;
    }
    - (float)getInterval{
        return motionManager.accelerometerUpdateInterval;
    }
    - (NSTimeInterval) getStamp{
        return motionManager.deviceMotion.timestamp;
    }
    - (void)setStartingAttitude: (CMAttitude*) ref{
        referenceAttitude = motionManager.deviceMotion.attitude;
    }
    - (bool)isDeviceReady{
        return motionManager.deviceMotionActive;
    }
    - (void)destroyMotionManager{
        [motionManager stopDeviceMotionUpdates];
        motionManager = nil;
    }
    
    @end
    

    and whenever I want to use it i can just declare a variable in that class header like this:

    MotionManagerSingleton *Manager;
    

    and use it like this on the m file:

    Manager = [MotionManagerSingleton sharedInstance];
    
    if ([Manager isDeviceReady]) {
        NSLog(@"Device Is Ready on Drawing View Controller");
    }
    
    referenceAttitude = [Manager getStartingAttitude];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edited again because it originally wasn't clear that I'm trying to initialize the arrays
how to initialize a private static member of a class in java. trying the
I'm trying to initialize a static class, with an argument, and then run some
I am trying to build a web interface for an embedded device, that will
i'm trying to initialize all cells of a matrix with NULL values, but something
I am trying to initialize my controls in Silverlight. I am looking for something
I'm trying to initialize string with iterators and something like this works: ifstream fin(tmp.txt);
I have a list string tag. I am trying to initialize a dictionary with
I'm trying to write a simple program in VC++ which will just initialize the
How do I initialize a 2D array in Perl? I am trying the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.