www.it-ebooks.info
iPhone Location Aware Apps
by Example
Beginner's Guide
Build ve complete locaon-enabled apps from scratch—from
idea to implementaon!
Zeeshan Chawdhary
BIRMINGHAM - MUMBAI
www.it-ebooks.info
iPhone Location Aware Apps by Example
Beginner's Guide
Copyright © 2012 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark informaon about all of the
companies and products menoned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this informaon.
First published: March 2012
Producon Reference: 1160312
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-224-3
Manu Joseph
Producon Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
www.it-ebooks.info
About the Author
Zeeshan Chawdhary has been a keen developer for the last six years, and has worked
in the locaon-based space for the past ve years. He is currently the Chief Technology
Ocer of Wcies Inc, a San Francisco-based Locaon Content Provider. He is currently
experimenng with PostGIS, PhoneGap, and iOS 5, and is currently trying his hand at
blogging again at .
I would like to thank all the lovely people at PacktPub, especially Mary
Nadar for having introduced me to the PacktPub family.
A special thanks to Leena, Susmita, Lubna, and Llewellyn for working with
me relessly on the book.
I would also thank Christopher D. Sloop from WeatherBug, Lauren Sperber
and Janine Iamunno from AOL Patch.com, Tim Breidigan, and Robert
Marndale from Evenul.com – for having allowed me to use their
respecve APIs in the book, you guys rock man!
www.it-ebooks.info
About the Reviewers
Sebasan Borggrewe, born and raised in Germany, is a computer science Master's
student at the University of Edinburgh/RWTH Aachen. Since he was 16, he has been
freelancing for several web and mobile agencies, and has founded his own agency.
Currently, he is the co-founder and CTO of Loyalli Ltd., a London-based startup, developing
mobile loyalty card soluons (
).
teams. He specializes in .NET, iOS, and Android mobile development for clients in the
Washington, DC metropolitan area.
He can be reached at
Shuxuan Nie is a SOA Consultant, specializing in SOA and Java technologies. She has more
than 10 years of experience in the IT industry that includes SOA technologies, such as BPEL,
ESB, SOAP, XML, and Enterprise Java technologies, Eclipse plugins, and other areas, such as
C++ cross-plaorm development.
Since 2010, Shuxuan has been working in Rubiconred, and has been helping customers
resolve integraon issues.
From 2007 to 2010, Shuxuan had been working in Oracle Global Customer Support team,
and focussed on helping customers solve their middleware/SOA integraon problems.
Before joining Orcale, Shuxuan had been working in IBM China Soware Development Lab
for four years as sta soware engineer, where she parcipated in several complex products
on IBM Lotus Workplace, Webshpere and Eclipse plaorm, and then joined the Australia
Bureau of Meteorology Research Center, which is responsible for implementaon of
Automated Thunderstorm Interacve Forecast System for Aviaon and Defence.
Shuxuan holds an MS in the Computer Science degree from Beijing University of Aeronaucs
and Astronaucs.
www.it-ebooks.info
Marn Selva heads the Gaming team at Hungama Digital Media Entertainment Pvt Ltd,
with over nine years of experience. His passion, experience, and experse have also helped
him develop a keen interest in Product Development for Online & Devices.
At Hungama, Marn is responsible for building a gaming portal called
www.thegamebox.
com
, and heads a Gaming Studio that comprises of iOS Developers, PHP Developers, Game
Designers, and Content Writers.
He can be reached at
and
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: The Locaon-based World 7
Understanding Locaon-based Services 8
Time for acon – consuming Locaon-based Services with Google 9
Buzzwords in the Locaon-based Industry 11
Applicaon of LBS and common use cases 13
Military 13
Government 13
Commercial 13
How Apple uses LBS in the iPhone, iPad, and iPod devices 14
iOS locaon API 15
Time for acon – turning o Locaon Tracking in your iPhone 15
Behind LBS – GPS 17
User segment 17
Space segment 18
Control segment 18
Push and Pull methods of Locaon Services 18
Push Service 18
Pull Service 19
Life without GPS: Wi-Fi-based locaon detecon 19
Life without GPS: cell ID posioning and cell tower triangulaon 21
Time for acon – using the SkyHook Wireless Loki framework to
determine your locaon 22
Life without GPS: Google Maps API 23
Understanding Indoor and Outdoor Navigaon 26
Summary 27
www.it-ebooks.info
Table of Contents
Region monitoring 73
Geocoding and reverse Geocoding – CLGeocoder 74
Direcon using heading 75
Core Locaon Manager – CLLocaonManager 76
Time for acon – checking for locaon service availability 77
User authorizaon 80
Time for acon – using Core Locaon with user authorizaon 81
The CLLocaon object 84
Time for acon – receiving locaon updates in your applicaon 85
Time for acon – boundary monitoring with Locaon Manager 87
Extending Hello Locaon for nearby events 90
Important things to know before we begin 91
www.it-ebooks.info
Table of Contents
[ iii ]
Time for acon – extending Hello Locaon for nearby events 91
Time for acon – Last.fm API in your app 97
What just happened? 98
Extending Hello Locaon for local search 99
Important things to know before we begin 100
Time for acon – building a local search app with foursquare 100
Summary 103
Chapter 4: Using Maps in your iOS apps—MapKit 105
Overview of the MapKit framework 105
Understanding map geometry 106
Time for acon – using MapKit in your app 108
Time for acon – using map gestures – panning and zooming 115
Annotang Maps – an overview 117
Time for acon – adding annotaons to your maps 117
Time for acon – draggable annotaons 119
Chapter 6: Events App—PacktEvents 179
PacktEvents: Overview and architecture 180
Architecture of PacktEvents 180
Storing and Retrieving Events with SQLite 181
Time for acon – storing and retrieving events with SQLite 182
Plong events on a map 191
Time for acon – plong events on a map 192
Filtering Events display by Event Categories 197
Time for acon – ltering Events by categories 198
Using the Event Kit framework to add events to your iPhone calendar 205
Time for acon – adding events to your iPhone calendar 207
Using the Twier framework 210
Time for acon – adding Twier capabilies to your iPhone app 210
Bonus: using the Layar Player API in your app: Augmented Reality 213
Time for acon – adding Augmented Reality to your iPhone app 213
PacktEvents: building the app 219
Summary 220
Chapter 7: Advanced Topics 221
Using direcons with locaon 222
Direcon using heading 222
Geng your app ready for direcon 222
Understanding heading using magnetometer 222
Time for acon – using heading for direcon in your app 223
Direcon using course 226
Time for acon – using course for direcon in your app 226
Core Moon: Moon Manager 229
How to use Core Moon 230
Time for acon – using MoonManager: accelerometer 231
Core Moon conclusion 235
Background app execuon 235
Find locaon by names 286
Time for acon – consuming the Patch News API 286
Adding the Geo Fencing support 295
Time for acon – adding the Geo Fencing support 296
Building our app - PacktNews 299
A bit on StoryBoard 299
Time for acon – building PacktNews 300
Summary 313
Chapter 10: Social Governance—TweetGovern 315
Social governance – an overview 316
TweetGovern – behind the scenes 316
Stackmob 318
Our approach: Twier 319
Icons and images 322
SDKs and frameworks 322
Time for acon – creang the UI for TweetGovern 323
Time for acon – detecng the user locaon and showing nearby issues 326
Time for acon – creang and vong for an issue 335
Summary 347
Appendix: Pop-Quiz Answers 349
Index 353
www.it-ebooks.info
www.it-ebooks.info
Preface
iPhone Locaon Aware Apps Beginner's Guide is probably the rst book from any technical
publisher that teaches you to build real world applicaons (ve of them). That's a bold step
from PacktPub - by undertaking more lively praccal examples, rather than 400 pages of
text! The book lays emphasis on locaon services, due to the ever-increasing role of locaon
in our day-to-day lives and increased geo-referenced content being produced/consumed on
the Internet and Mobiles. Be it news, sports or gossip, consumers no longer want to read/
Chapter 5, Weather App—WeatherPackt, builds a complete Weather App using WeatherBug
API. It also provides a Sengs page in the app to customize the Weather display. As a bonus
to the readers, we also included the Nuance Speech SDK for reading out the weather!
Chapter 6, Events App—PacktEvents, builds an Events app that shows us nearby events,
concerts, and gigs by Arsts, by using the excellent Evenul.com API. This chapter also shows
how to use the Twier API in iOS 5, and gives us a taste of Augmented Reality with the Layar
Player SDK.
Chapter 7, Advanced Topics, teaches us using direcons with locaon background services
including background locaon. It also explores the Moon Manager in iOS SDK, along with
Push and Local nocaons.
Chapter 8, Local Search—PacktLocal, works with the foursquare API to build a local search
app, with geo-fencing support.
Chapter 9, Locaon Aware News—PacktNews, uses the AOL's Patch News API to build a
hyperlocal news app. It uses the new iOS 5 Storyboarding feature in this applicaon, with
support for oine content using SQLite.
Chapter 10, Social Governance—TweetGovern. Twier provides the backbone for this
chapter and the accompanying app. We use Twier and hashtags for building the business
logic for our social governance app tled
tweetgovern. We learn to use the twier
re-tweeng concept as well, building upon our business logic.
What you need for this book
To run the examples and apps provided in the book, you will need a Mac running on Intel
Architecture with Xcode 4.2 or higher and iOS 5 installed on your iPhone or iPad.
Some examples need an API key, which is duly menoned at the beginning of the
chapter/topic.
www.it-ebooks.info
Preface
[ 3 ]
Who this book is for
Novice to professional level iOS programmers, who want to master locaon awareness and
}
www.it-ebooks.info
Preface
[ 4 ]
Any command-line input or output is wrien as follows:
2011-09-04 16:40:09.421 Hello Location GeoNames[3896:f803] Location
Inserted Cupertino
2011-09-04 16:40:33.977 Hello Location GeoNames[3896:f803] Location
Inserted Soho
2011-09-04 16:40:42.230 Hello Location GeoNames[3896:f803] Location
Inserted Wadala
2011-09-04 16:40:48.889 Hello Location GeoNames[3896:f803] Location
Inserted Cupertino
2011-09-04 16:40:55.913 Hello Location GeoNames[3896:f803] Location
Inserted Financial District
2011-09-04 16:41:04.692 Hello Location GeoNames[3896:f803] Location
Inserted Sydney CBD
New terms and important words are shown in bold. Words that you see on the screen,
in menus or dialog boxes for example, appear in the text like this: "Enter Hotels in San
Francisco as the search key and hit Enter".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to
develop tles that you really get the most out of.
To send us general feedback, simply send an e-mail to
, and
menon the book tle through the subject of your message.
If there is a topic that you have experse in and you are interested in either wring or
address or website name immediately so that we can pursue a remedy.
Please contact us at
with a link to the suspected pirated material.
We appreciate your help in protecng our authors, and our ability to bring you
valuable content.
Questions
You can contact us at if you are having a problem with any
aspect of the book, and we will do our best to address it.
www.it-ebooks.info
www.it-ebooks.info
1
The Location-based World
Locaon-Based Services will be worth $10 Billion by 2016 – GigaOm
Locaon-Based Services (LBS) are a revoluonary, but sll fresh from the oven,
breed of services that has grown tremendously to carve itself as a new industry
in just a few years.
Locaon-Based Services is the next step in the evoluon for search, on the web
and mobile, adding the Locaon Context (where am I or things around me) for
search. To quote from Wikipedia on the denion of LBS:
A Locaon-Based Services (LBS) is an informaon or entertainment service,
accessible with mobile devices through the mobile network and ulizing the
ability to make use of the geographical posion (read Geocodes or Latude/
Longitude) of the mobile device.
You may have already used LBS when on Twier, Facebook, Foursquare,
Groupon, or visit hyperlocal web pages such as
Wcities.com, Yelp.com,
Qype.co.uk, and Eventful.com to nd the top venues in the city or events
happening in your city.
Want to know how the locaon is determined? Connue reading the chapter to
understand the dierent locaon detecon methods and which one is the right
own posioning system as well, but GPS remains the most popular and preferred choice for
device makers and applicaon developers worldwide.
Anyone can use GPS freely by using either a Personal Navigaon Device (Garmin, TomTom),
or an In-Car Navigaon System (Ford Sync), or by using a Smart Phone.
On the mobile front, LBS also use Google Maps and other cartographic API services
extensively (even in cases where the device does not support GPS). This is done using rich
map data and Geocoding services. Using Geocoding and smart algorithms, a user's posion
can be guessed or approximated. Mobile Operang Systems, such as Android, further the
cause of LBS by integrang locaons into the Core OS, where the locaon can be fetched,
used, and updated by all applicaons.
Apple iOS leads the pack with the best soware API support, coupled with excellent
hardware and posioning system integrated in the Apple Eco System. It also has
network-based Assisted GPS (AGPS) that uses the network's data connecon in the
case of weak GPS signals as well as Apple's own Wi-Fi locaon database. iOS developers
have a plethora of locaon tools and API to work with.
www.it-ebooks.info
Chapter 1
[ 9 ]
In short, LBS can be described as a combinaon of two components, Locaon Providers
and Locaon Consumers, with GPS, AGPS, iOS API, and Google Maps API as the locaon
providers and GPS receivers, mobile phones, and websites as the consumers of locaon data.
Time for action – consuming Location-based Services with
Google
To understand how LBS work behind the scenes, let's take an example of the most common
use of LBS, that is, how Google.com uses LBS for its search.
1. Fire up your Safari Browser and navigate to .
2. Enter Hotels in San Francisco as the search key and hit Enter.
3. You are presented with results from the Google Places database, as shown in the
following screenshot:
www.it-ebooks.info