Search code examples
iosgpslocationaccelerometer

Multiple sensors in iOS


I have two different POC, one for the accelerometer and one for GPS. However, I am not comprehending the architecture to put marry both of the applications. I need to initialize both the accel and GPS when the application loads. I have the main view tied to the accel, but also need to the the location of the device.

My current architecture is Projects in workspace

  • Main App
  • Utility App
  • Services App
  • Domain App

The main app ViewController inherits

: UIViewController

That all wires up correctly the the accel works as expected.

In the Utility CoreLocationUtility class, I have it inheriting the CLLocationManagerDelegate.

The question is, how do I register a delegate from the same view that is of type AccelDelegate?


Solution

  • If you want to make your ViewController act as delegate for both accelerometer and GPS, state in its header file that it obeys both delegate protocols:

    @interface ViewController : UIViewController <CLLocationManagerDelegate, UIAccelerometerDelegate> {
        CLLocationManager *myLocationManager; // an instance variable
    }
     // ... your definitions
    @end
    

    then somewhere in your ViewController

    [UIAccelerometer sharedAccelerometer].delegate = self;
    myLocationManager = [[CLLocationManager alloc] init];
    myLocationManager.delegate = self;
    

    then somewhere else in your ViewController, put the delegate methods for both protocols

    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
        // ... your code
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
       // ... your code
    

    }

    and that should work, although there may be typos, I have not compiled this. Plus, in my experience, location manager code tends to get quite big. You may be much better off putting it in a class of its own, to be instantiated by the ViewController.

    Can anyone explain why the only method in the UIAccelerometerDelegate protocol is deprecated?