Join Steven Lipton for an in-depth discussion in this video Set up the project, part of Advanced iOS App Development: Core Motion.
- [Narrator] With some basic concepts behind you, you're ready to work with Core Motion. Our strategy for learning Core Motion is to start with a high level API and work down to the lower levels closer to the sensors. I'll start with a very useful class in core motion, the pedometer. From fitness apps to games, the pedometer counts steps and calculates related data, such as pace and distance. It uses elements from both Core Motion and Core Location, which you never have to touch Core Location to use those elements. I'll create a simple pedometer that will measure your steps, distance and pace.
Go to Xcode. Create a new Xcode project. Select IOS, single view application. Hit next. Make the name Core Motion Pedometer. Select the correct team, organization name, and organization identifier. For the language, use Swift. For the devices, use an iPhone. Make sure that Core Data and the Unit Tests and the UI Tests are selected off.
Hit next. And save it somewhere. When the application loads, change the display name to pedometer. This will give a smaller name for the icon. In this application, you may need to test off of Xcode and this will help you find your app better. Speaking of icons, I'll load an icon in launch image for this application. You'll find them in the downloads folder under assets. In Xcode, open the assets.xcassets folder and click on AppIcon.
Drag the AppIcon 2x to the iPhone app, 2x. And the iPhone app 3x to the 3x. Drag the launch image to underneath where it says AppIcon. Go to the LaunchScreen storyboard. Select the media button. Drag the launch image into the view. And using auto layout with constraint to margins off, type 0 tab, 0 tab, 0 tab, and 0 tab.
Add four constraints. Go to the content mode and select aspect fit. You now have your launch screen. Core Motion uses several motions that Apple considers private data. On a pedometer, you're also using location data in the background. So privacy's a very important issue. You need to add a key NS motion usage description in the info.plist to ask permission of the user. Go the the info.plist and next to information property list, click the plus.
Scroll down to find privacy motion usage description. Select it. This is a key with a string value. The string will be a body of an alert sent to the user on their first use. It will ask them to accept the privacy settings for the pedometer. For the value, put in, this is a step counter and we'd like your permission to track motion.
While the icons and launch screen are important, the critical thing you must do for a Core Motion app, is include this key in your info.plist. Apple is serious about privacy. Forgetting it will get your app rejected and it may not work at all.
- 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)