An iPhone API for Accessing Google’s Secret Weather Service

July 21, 2011

Google’s “secret” weather service has been around for a few years now, and it still boggles the minds of most people who are quite familiar with the service.

Google has warned people against using the service, as it is officially unofficial and undocumented.

For folks who have no idea of what this stuff is, try the link below:

Assuming your internet connection is active (and working of course), you’d probably see XML data thrown back at you by Google.

Breaking the XML reply into 6 main nodes, we have:

1. <forecast_information>

2. <current_conditions>

3. <forecast_conditions> (We actually have four of these nodes)

XML is simple, yet iOS developers need more than raw XML data. They need them as objects for manipulation!

iOS developers need data in the form of NSStrings, NSNumbers, NSDates, NSURLs!

I’ve googled my way in trying to find an Objective-C based iOS API that is up for the whole task of parsing the XML reply and storing the reply elements as objects, unfortunately, to no avail. So I decided to make my own, with the help of a popular open-source XML parser for Mac/iOS, TBXML. So, why didn’t I use Apple’s native XML parser class? Simply put, I don’t like its event-driven approach. It simply does not fit the bill in this application.

Let’s get started!


I am assuming that the readers of this post are more than capable of creating a new project in Xcode 4, so let’s skip that.

First, we must include 2 important entities into our project:

1. libz.dylib (Don’t worry, it’s just a compression library used by TBXML). Click on your Project Name > Project Name under Targets > Build Phases. This will load the screen below. Click the + sign in Link Binary with Libraries, and add libz.dylib.

2. GoogleWeatherAPI.h

Our header file contains the method needed to initialize our API, initiate requests to Google, and receive data as convenient objects. Any object utilizing a GoogleWeatherAPI class must conform to the GoogleWeatherAPIProtocol. We’ll discuss the protocol list in the later sections.

Initializing A Request

When we want to request weather information from Google, we simply instantiate the GoogleWeatherAPI object with a delegate and the target location. We are using “Yokohama” as our location of interest in the sample code below. After instantiating, we call the start method of GoogleWeatherAPIto initialize our request with the proper parameters. Note that whitespaces in targetLocation string are automatically converted into plus signs by the API.

Checking for the Validity of the Response using Protocol Methods

You must implement all 3 delegate methods above to retrieve the status of the request-reply sequence.

The first method, didReceiveReplyForTargetLocation:fromURL, is triggered when parsing of the XML reply was successful.

The second method, failedToReceiveReplyForTargetLocation:withURL, is called when a connection problem occured or the location passed in the argument did not contain weather information.

Lastly, didReceiveInvalidReplyForTargetLocation:withURL is triggered when incomplete XML data has been retrieved.

From XML Data to NSStrings, NSNumbers, NSDates, NSURLs

In case you still have a difficult time imagining the whole picture, just look at the Xcode debugger snapshots below. These snapshots were taken after the XML data have been handled by the API.

Some Convenience Methods for Temperature and Date

I have added some convenience methods as well to keep everyone happy.

This API allows you to retrieve the day of the week given an NSDate object.

Furthermore, this API allows you to convert celsius data to fahrenheit and vice-versa, all using NSNumber objects.

Download the API

Download the API from here:

This is JP Yao, and I hope you learned from the article.