Learning How to Create iOS AppsSubscribe Now

How to use the iPhone Digital Compass in your App

iOS devices have several sensors built in that you can make use of in your app. One of them is the digital compass which is also known as a magnetometer. In todays tutorial I want to show you how how you can access the data so that you can use it in your app. The app will be very simple and will just use 8 UILabels with 4 of them being updated with various values.

headingTo begin with, create a new project in Xcode and choose a Single View Application. If you are not sure how to do this, take a look at my post called Setting up your First iOS Xcode Project which will get you to a fresh start and ready to create the app.

The next step is to drag our 8 UILabels. Put four of them down the left side of the screen and the other four down the right side as pictured here (aligning the text on the right hand labels to the left). Change the names to something like seen in the screen shot. You do not have to set the right hand set at zeros… anything will do, including blank UILabels.

Next, click on the middle Editor button (top right) to create a split to allow two files to be opened. On the left, click the Storyboard. The ViewController header file should then appear on the right.

At the top of the header file, import CoreLocation and also add the CLLocationManagerDelegate as follows:

1
2
3
#import <corelocation /CoreLocation.h>
 
@interface ViewController : UIViewController <cllocationmanagerdelegate></cllocationmanagerdelegate></corelocation>

The first of these two lines indicates that we will be using some of the CoreLocation framework. The allows this ViewController to receive updates when they are available. [Read more...]

How to Make didUpdateLocations Compatible with iOS 5 and iOS 6

A popular method used from the CLLocationManagerDelegate protocol is - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation which allows your app to receive updates when any location changes are detected. The new location details were stored in the newLocation which is a CLLocation.

When iOS 6 launched, that method was deprecated with a suggestion to use a newer version called - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations or in short locationManager:didUpdateLocations:.

This quick tutorial has been created to explain how to handle a deprecated method, what should be done about it and where you can find out more details about the changes. If you want to know how to use the newer locationManager:didUpdateLocations: method then take a look at the didUpdateLocations tutorial that explains how to work with the NSArray provided by that method.

deprecated-method [Read more...]

Getting the Timestamp from CLLocation

A quick tutorial today. I was recently asked how to get a timestamp from a CLLocation object, particularly when the -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method was used in the CLLocationManagerDelegate protocol. As with most programming solutions, there’s always more than one way to accomplish this. Today, I’ll show you one of the ways it can be done.

Assuming you have your CLLocationManagerDelegate all configured correctly and that you also have your CLLocationManager, this is one way you can get the timestamp from the CLLocation update. [Read more...]

DidUpdateLocations iOS Example

When iOS 6 launched last year, Apple deprecated a commonly used method from the CLLocationManagerDelegate protocol. The method deprecated is - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation and is used to tell the delegate that a new location is available. Each time a location change is detected, this method is called and within the method, you can trigger certain methods or updates to happen as needed.

This was done by using the (CLLocation *)newLocation details. With this method being deprecated in iOS 6, Apple recommends you now use a newer method called - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations. Rather than providing a newLocation as a CLLocation, this new method returns an NSArray of locations. As the value is a different type, you will need to modify your code to account for the data being handed back in an NSArray.

The good news is that according to the documentation, you still get a CLLocation object, but instead of accessing it direct, you need to pull it out of the NSArray to action it. Lets take a look at some code that will help you do just that. [Read more...]

iOS 6 Core Location Tutorial

iOS developers get access to a number of frameworks to help them develop apps. One of the frameworks I like to use is Core Location, often joined with MapKit. The tutorial today will focus on just the Core Location framework and later in the series, we’ll add MapKit to the project so that you can mark your location on a map.

For the project, I’ll be developing for iOS 6 using Storyboards as well as ARC. So lets get going:

Creating the Project

First, lets create a new project by opening Xcode and clicking File > New > Project. I often opt for an empty application, but in this instance I’ll choose a Single View Application because this is all we need to demonstrate a way of working with Core Location.

new-project [Read more...]

How to Create a Subclass and Set the View Controller’s Custom Class

When working with Storyboards in Xcode, you’ll quickly find that you end up with varying amounts of different View Controllers (depending on the size of your app). Each of these needs to have a subclass created and then that subclass needs to be set as the View Controllers custom class. I’ll explain how that is done today, along with a way that you can find out exactly which type of View Controller you need to subclass.

Dragging Out the View Controller

For todays post, I’ll concentrate on the iPad as there are more View Controllers available for this device. The same principles apply for the iPhone although you wont be able to work with a SplitView Controller for example as this is an iPad only View Controller.

First of all, we need to drag out a View Controller to the storyboard. Lets go for the Table View Controller as pictured below.

UITableView [Read more...]

One Way to Fix the Apple Mach-o Linker Error

Bugs and errors can be frustrating to fix. Unfortunately, bugs are going to happen and you will see errors from time to time. Often errors appear because of the smallest detail.

One such error that popped up when I was building a project was the “Apple mach-o linker error”. It took me about an hour to eventually track it down. Here is what you will see on screen with this error:

apple-mach-o-linker-error [Read more...]

A Quick Look at Xcode Documentation and How to Understand It

The documentation provided with Xcode is extremely helpful. It provides a lot of the information you’ll need to create apps for iOS devices. If you want to know how a particular class works, you look in the documentation to find exactly what is required and what methods are available. Understanding the documentation is one of the keys to being able to successfully start and finish writing an app.

How to Access the Documentation

You can access the documentation in several ways. This includes the web and the Xcode organizer as well as snippets of text that appear when typing in method names. Lets take a quick look at each so that you can get a good understanding of where to find the documentation.

corelocation-searchOn the web, you simply need to search for something like “corelocation class reference”. To find the official documentation, look for search results from the developer.apple.com domain name. Typing in the above search term shows the results (image to the left). The first result is the Framework reference which lists the Class References found within that framework. You can select this one, but if you want the corelocation class reference (called CLLocation and CLLocationManager) then you’ll need to select one of them on the next page.

When loading up the actual class reference for CLLocation, you will see the overview and tasks as well as what the class inherits from.

[Read more...]

iOS App Development for Non-Programmers is an Excellent Resource for Learners

After spending several months learning Objective-C, I still found there were a few things that eluded me. This included things like not fully understanding the different between a view and a view controller on to basics such as how to read and understand the Apple iOS documentation and how to read the different methods when they pop up on auto-complete.

In December 2012 I thankfully came across a book called iOS App Development for Non-Programmers while reading the news on Zite. I had a look at the post, noticed that the book (I thought that there was just 1 at the time) was available to download on the iPad in iBooks. I decided to download a free sample to see what it was all about.

I cannot remember the specifics, but I think the first 4 chapters were available for free. I read them in the first evening within a few hours of downloading the book and thanks to the simple delivery of the content, I finally grasped what a View and View Controller was to the point where I could picture in my mind how they worked and how they were related to each other. I of course knew before what a View did, but understanding the relationship between the two was key for me.

iOS-Apps-For-Non-Programmers [Read more...]

Happy New Year – Back to Work

Well, it has been a good few quite weeks in the office thanks to Christmas and the New Year. With it being the 2nd Jan, it’s time to wish you all a happy new year and let you know I’ll be diving back in to programming today.

Although I stepped away from the blog for almost a couple of weeks, I still spent some time learning and practicing. Over the past week I have learned more about views, view controllers as well as other types of controllers such as table view controllers. I have also learned a bit more about storyboarding on Xcode and how you can create a prototype app with little to no coding.

Today, I’ll be diving back in to some iOS/Xcode things and later on this week, I’ll post a few more things from my study notes of CS106A. [Read more...]