Join Steven Lipton for an in-depth discussion in this video Detect devices, part of Advanced iOS App Development: Core Motion.
- [Instructor] You saw with the pedometer that checking for sensors is important. Once again, you'll want to check for sensor availability in the user's device. This should always be the first step in launching your app: letting the user know they can't use this on their device. Go to ViewController.swift, close up the attributes inspector and the navigator. We won't be using either one of them. In ViewController go ahead and put in the import CoreMotion just under UIKit.
There's a class CMMotionManager that manages device availability, pushing and pulling of sensor data. We'll use the Core Motion Manager for the gyro and accelerometer. Add the motion manager to the class. Let motionManager = CMMotionManager. Our first order of business is to check if the device has the sensors.
We'll send an alert if any of the devices are unavailable. I'll also return that value so the rest of the app can decide to launch. So we're going to go ahead and add a function; isDevicesAvailable and it will return a Bool. There's two ways you could check for availability. One is to check for the gyro and accelerometer. We can use an isAvailable property to get their availability like this.
Let gyroAvailable = motionManager. isGyroAvailable. Let accelAvailable = motionManager. isAccelerometerAvailable. CoreMotionManager bundles both in one property.
Command out those two lines. Add this instead; return motionmanager. isDeviceMotionAvailable. I'll set up my alert to tell the user that I don't have the correct devices. Above the return motionManager line, add the following: if !motionManager. isDeviceMotionAvailable.
And the alert by baking an alert controller. Let alert = UIAlertController, and we're going to use the one title, message, preferredStyle. And the title will be "Fencing", the message will be, "Your device does not have "the necessary sensors." "You might want to try on another device." And the preferredStyle will be an alert.
I'm going to just go ahead and present that. Now the alert, animated: true, completion: nil. So what we're going to do is I'll make an alert, I'll put out the message, and then I'll just present it. I'll also go ahead just under this, and to the console so I know I'm doing it. "Devices not detected", okay.
I just found a mistake, there we go. You'll notice I intentionally forgot something. There's no action in the alert, there's just an OK button. This is one of those rare circumstances that you don't want the user to proceed, we'll launch our app directly from the ViewController. We're going to add a viewDidAppear here. And the reason we're going to use viewDidAppear is because this alert has to occur once the story board is already loaded.
So we've got super.viewDidAppear(animated), and then I'll go ahead and put if isDevicesAvailable. And I'll just print a message, "Core Motion Launched". Okay. Simulators do not have sensors, so we're going to use the iPhone 7 Plus simulator which will fail the sensor test.
So go ahead and build and run the application. This will launch and you should get that alert. In the case of a device that does not have the sensors required, it's a style decision to do what I did here and stop at this message, or have an OK message dismiss the alert and have an app that does nothing. I prefer blocking the user completely. Either way, inform the user that they can't have a device due to a lack of sensors is an important practice when using Core Motion.
- Reading Core Motion data
- Understanding Core Motion methods
- Creating a pedometer app
- Using pace and distance data from Core Motion
- Pushing and pulling device motion data
- Accessing the altitude sensors
- Working with the motion activity manager (CMMotionActivityManager)