Sponsors

 
Showing posts with label iPad. Show all posts
Showing posts with label iPad. Show all posts

Sunday, May 16, 2010

Tutorial: Creating Class Categories in Objective C

As I began refining some sample code for making Universal Apps for the iPhone and iPad, I sought a way to make my code as concise as possible. I needed to determine if the app was running on an iPad or iPhone/iPod Touch and use it to my advantage. This basic information is contained in the UIDevice class.

Unfortunately, using:


         [UIDevice currentDevice].model    

did not do precisely what I needed to do. I needed something specialized. I wanted to add new functionality to the UIDevice class as if it had been there all along.

Looking for Solutions
A few months back I ran into a code sample that adds functionality to the NSDate class called NSDate+Helper. This class, created by a programmer named Billy Gray was my first exposure to a wonderful feature built into Objective C called Class Categories. When I saw how one could do this, it set the wheels turning in my mind at all the possibilities!

In this tutorial I am going to show you how to use class categories, and for our example we are going to add functionality to the UIDevice class for use in ultimately creating a sample Universal App.

Note: The tutorial video appears below. If you are reading this post from a feed that does not support showing the embedded video click here to jump directly to this post on iPhone Obsessed.


note that the above video contents are easier to see in full screen mode.

Sample Code Info:

Getting Sample Project Source Code & Running it in the Simulator
It would be handy for you to download a copy of our Universal App project to look over and try out. In order to really use it you will need to have an Intel-based Mac computer, and XCode 3.2 or better installed on it. Once you have done this,  you will be able to run it in the iPhone Simulator.

Make sure when running on the simulator that you set the proper build version before doing a build:

Simulating the app running on an iPhone:

Simulating the app running on an iPad:


Running the Sample Project on an Actual Device
If you want to run the project on an actual device rather than just the simulator, you will need to be a paying member of the Apple Developer Network ($99 a year at the time of this post). And you will need to have gone through the hoops of setting up your certificates, app record, and provisioning. ... We are not covering any of this getting on the device stuff in this tutorial.

At least you can run the project in the iPhone Simulator for free! 

Getting In Class Categories

What is a Class Category?
  • You use class categories to add new functionality to an existing class. This could be one of your own classes, or it could be a class in Apple or someone else's API.
  • You do not have to sub-class the class you are adding to.
  • You can add the new functionality in such a way (inside your project) that it seems that the functionality was there all along!

Step 1: Come Up With A Category Name
When creating a class category you are adding some sort of new functionality to an existing class (without touching any of the code actually in the existing class)! If you were to describe in a general way what that new functionality was, how would you phrase it? What "category" would it fall under?  This is something you make up. I make up some sort of camel-cased phrase for my category name. In the case of my new functionality to add to UIDevice, I thought that MyDeviceType captured it well. Perhaps if I had thought about it longer I could have made up something better! But that is what I came up with so that is what I used!

Step 2: Create a Group in Our Project to Place our Class Category in (optional)
The first thing you may want to do is create a new group (folder in your project) to place it in. This is optional, but it can make finding it easier as well as providing some form of internal documentation.

So, for the sake of argument, let's say that this is what you want to do. Let's go through the steps needed to do this:
- First of all click on your project file item (in examples shown in the screen shots, this is "classcat").
- Then control-click on it again to bring up a pop-up menu.
- Click the Add option at the top of the menu...
- Then click the New Group option (see screen shot below)
At this point you will see a new group appear under classcat with the text highlighted and ready for you to edit like in the screen shot below:

Lets rename the group to "Class Categories"...

Step 3: Create a new Class from the NSObject Class
- Control-click on our Class Categories group to bring up the pop-up menu.
- Pick the Add option.
- Pick the New File... option.


- The New File dialog window will appear.
- Make sure that Cocoa Touch Class is highlighted on the left hand side.
- Make sure the Objective-C class option is selected.
- Make sure Subclass of is set to NSObject.
- Then click the Next button.


The next screen will come up. It is looking for you to name the class:


We need to enter our class name. Gotta have some sorta naming convention! We will use the name of the original class we are creating a category for followed by the name of the category itself. That sounds good! So the original class in our example is UIDevice. The name of our class category is going to be: MyDeviceType. So we will type in UIDeviceMyDeviceType.
Above I type in my new class name. Then I would just click the blue Finish button.

Here is the initial header file XCode created:

This file starts out as just a normal class being subclassed from a superclass (NSObject).

Step 4: Modify Our New Class Files and Turn them Into a Class Category
The first thing we will do here is replace the class name UIDeviceMyDeviceType with the class name we are creating a category for. In this case that is UIDevice.
Next we are going to put MyDeviceType in parenthesis and delete the colon, NSObject and the curly braces and end up with this:
 
- Again note that above UIDevice is the existing class we are creating a category for. 
- MyDeviceType is the category name we made up for our category.
- The category name is in parenthesis.
- Although it might seem that we should put the category name in quotes... we do not.
- Again, we removed the normally correct curly braces... to leave them in on our class category will get you errors when you build your project.

While we are at it, lets add some interface definitions for the methods in our class category that we want to make public:
 

There may be other members in our class category than these, but these are available to be used by the "outside world."

Now let's look at the implementation file (the .m file)...



Notice it has the class name we gave originally. After the text UIDevice, we need to put parenthesis around MyDeviceType... Again, no quotes go around the class category name.



There! It is now setup to be used as a class category! All that remains to be done is add any variable declarations and code for our members.

Here is a screen shot showing a partial view of what that could look like:
 

To see an actual working example download the Universal App sample code I have provided.

Step 5: Use New Functionality in Your Project!
The new functionality at this point is ready to be used. There are two ways to make it accessible to various classes in your project:

1. In the implementation file of a class that is going to use this functionality, add an #import line at the top. In our case the line would be:

     #import "UIDeviceMyDeviceType.h"

2. If you are going to use it widely in your project there is a better way! 
Click in the Other Sources group in the project and click on the classcat_Prefix.pch file.
(the first part of the file name is the project name).
 

You will see two import lines defined for Apple framework stuff. XCode puts these in so that the programmer does not have to use #import for anything in these frameworks throughout the project... the #import is implied. Underneath the #import add our import statement:

     #import "UIDeviceMyDeviceType.h"




Save your changes and BOOM! All your new functionality in your new class category is available to project!

You could say:

      NSString *myDev = [UIDevice myDeviceType];

 in your code anywhere in your project and the new myDeviceType method will just work!

Different Uses for Class Categories
Here are some possibilities to think about:
  • Add new functionality to existing classes (especially API type stuff) like we did in this tutorial.
  • Break a large class up into separate files grouping various blocks by function "category."
  • Can you think of anything else? :)
Hope you find this tutorial useful. Any questions? Ask away. If you could rate my Youtube video that would be great too! 

In the next tutorial we will be talking about the structure of a Universal App.

Saturday, May 1, 2010

The iPad 3G is Launched


Friday, April 30th, I took the day off in order that I could buy my very own iPad with 3G built in. I'd been showing some restraint. I had come close to just saying the heck with it I'll get the Wifi only version. But I toughed it out.

So I took the day off of work so that I could be there bright and early. I dropped my wife off at work so that I could have the car. But when I got to the store I found out that the new 3G iPads would not be going on sale until 5pm later that day! They were closing up the store an hour before the event to get ready. Pooh!

I decided to get all the accessories that I wanted for my new iPad right then and there and come back to purchase said device.

Getting the iPad
Well I got to the store there was a line of people snaking around patiently waiting for their turn to get their new Apple toy. Quite a few people there. This wasn't an Apple store in California, New York, or even a store like the one on Michigan Avenue in downtown Chicago, this was in a mall in Oak brook, Illinois. Must be all the geeks! Well... I'm one of them!

Apple employees making plans just before the
store opens for the special iPad event.
People waiting in line near the store entrance.

Another view of the line waiting at the Apple
Oakbrook store.


Picture taken near the end of the line where I wait for my 
chance to own an iPad 3G.

When the store opened and the first few people were picked to enter the store, the Apple employees cheered and applauded. I'm glad they just did this at the start or I would have been embarrassed. As Apple employees would become available they would come out of the store and get the next customer in line. Overall the buying experience was very good and the line moved along nicely. They were out of the low-end 16 Gb 3G models before I got to the front of the line!

And what did I get? The 64gig 3G model (which was what I had decided to get all along). 

Monday, April 12, 2010

Some Initial Thoughts on the iPad, and I've Published an iPad App in the App Store!


I'm waiting for the end of this month to buy the iPad with 3G built in. And yet I bought an iPad on Saturday! What gives? Sometimes I wonder myself! Well, I bought it as a gift for a friend who just had a birthday. And, I must confess, I wanted to play with it before I gave it away!

I can say I really like it a lot! The eBook reader is very cool. Doing a search in the bookstore for "free" brings up lots of titles that I would want to read. And...

And I got to test out the app I did for the iPad on it. I bought it from the App Store and tried it out. It was the first time I had ever run the app on an actual device! Before it was just using the iPad Simulator included in the SDK. That was very cool! The app is a 56 page long eBook full of poems called "I Lost My Underwear Today and other flights of imagination." The owner is charging $1.99 for it. Unlike just a plain ol' book, you not only see the pages of text and illustrations, you can hear the author reading each poem by pressing the Play button at the bottom of the page.

If you would do me a favor, go and buy it, read it, listen to it, rate it, and leave a review. The guy who wrote the book is a great guy and could use the revenue. I will not be receiving any money myself from the purchases.

Anyway, I took the paper version of the book and converted it into an iPad version, recorded the author reading his poems, and put the whole thing together. It was cool working on my first iPad app.

Also cool was seeing it in the App Store. Nice! This is my first app in there. I think this is rather funny for a couple reasons:
  • First, I've been working on and off on a couple of iPhone/iPod Touch apps on and off now for over a year and am still not finished. These apps will be published by me, and God willing, I will get the revenue from them after Apple gets their 30%. But this new app up now in iTunes isn't my app! I wrote it for someone else!
  • Second, even though I have run my own apps on my iPhone to test them, I never had a chance to with this new app. As mentioned before I ran it on a device only after it was in the App Store!
Some Other iPad Thoughts
Talking about something other than the iPad App I wrote (I admit I'm a little giddy), there are some other observations I want to mention about the iPad that I played with.

You can't just use it out of the box, you have to hook it up to your computer and fire up iTunes to set it up for the first time. If on a Mac, iTunes will probably come up automatically when you plug it in (but you probably already knew that).

Saying that thought, this configuration process is very fast and easy.

I did note that the battery was at about 86% right out of the box, so, if that is average, you can begin using it right away. No charging needed ahead of time.

The bookstore is really cool as mentioned before. A lot of the books that were free are classic that I have read and like, or ones that I want to read. I am a big book reader! Boy, I can't wait to get my own iPad at the end of this month!

Saturday, April 3, 2010

Apple Rolls Out the iPad

Today, was the day that Apple fans have been waiting for. The day the iPad finally went on sale. I went to Bally's to workout and then decided to head over to the nearest Apple store. My intentions were not to buy an iPad but merely see what was up, and try one out for myself. My plans were to buy the 3G version that is supposed to come out later this month.

I knew there would be lines at Apple stores in places like Palo Alto, San Francisco, and New York. I did not think there would be such a thing really at the store in Oak Brook, Illinois.

I got to the store around 1-1:30-ish in the afternoon. And the scene I saw is shown in the picture below:

Front of Apple Store for the iPad Release

The lines were gone by now, but I asked someone working at the store about them, and there was a fairly long line waiting outside, ready for the Apple store to open its doors. Even at the time I shot this picture with my iPhone the doors were propped wide open and people were spilling out of the store (its a little hard to see in this photo). And most of the people were towards the front (where the demo iPads were on display). All the Apple staff for the store were out in full force, and, even with all the people who showed up, there were plenty of staff members there to serve you and answer your questions.

The place had a festive atmosphere and I started getting strong urges to buy a Wifi only iPad right there on the spot!

At the tables where the tethered iPad demo units were on display, you actually had to stand in an impromptu line to get a chance to use one. I took the picture below as I was waiting to get my chance. I heard from one lady who worked there that there had been a customer who had hogged using one for nearly an hour! Fortunately for me, everyone I saw was polite and civil!
They look so thin! I was surprised that when I actually picked one up, it was heavier than it looked. But its still cool!


People Giving the iPad a Try

At the back of the store, behind the counter, they had iPad boxes stacked up in stack upon stack. Though most of them looked like they were reserved for people who pre-ordered.

No docking devices with or without keyboards were to be seen, but that was not surprising considering information on the web said they would come out later. What was surprising (at least to me) is that there were covers on sales for the iPad. These were supposed to be delayed too. But, at least in this store, they had plenty available. Below is a rather blurry picture I took of the covers for sales hanging on a display on the wall:


Blurry shot of iPad covers that are on sale

I came really close to buying one, but I walked out of the store escaping the energetic atmosphere of iPad seduction that permeated the place. Does that mean I'm not getting one?
Nope! I'm waiting... at least for now!

Friday, February 5, 2010

I want an iPad

Well, several days have gone by since Apple announced it's new tablet device the iPad. It seems that all that could be said about a device that we can't have yet has been said. The iPhone Obsessed blog has remained silent. Well one thing you didn't know before: I want one! Not that anyone cares but me! One thing that I have wished for when programming for the iPhone is more screen real estate. This cannot be underestimated in importance. The iPad addresses this problem in spades. So I talk here, hopefully providing just a slightly different angle of looking at a subject that has already been written about at length.

As I read all the buzz for the iPad: the initial disappointment after the device didn't meet people's heightened expectations due to all the hype, then the growing realization by developers that they have all this space to put their user interfaces in... I see others are focusing in on this advantage. Some say that "look, a laptop has a bigger screen." But a laptop computer does not have a touch screen. A laptop computer has this physical keyboard that gets in the way and makes it feel a bit awkward sitting on your lap. When I saw Steve Jobs during the key note sitting in the chair with the iPad in his lap just using it, it connected with me: This was precisely like how I would interact with a book or magazine, or a pad of paper. It is not almost there like a laptop computer is. The awkwardness has been completely removed.

Steve Jobs called it an "intimate" experience. I have to agree. It worked for him as a real-world, non-tech device would work, and has worked for thousands of years. In fact, it works better than books and magazines do (no more holding the page down to keep it from turning when you don't want it to, no more trying to put a crease in the page so it stays open... more like a clay tablet with ancient cuneiform written on it. A flat, single surface. The iPad is like that except its able to hold lots more information, does color, is back-lit, and is hooked up to the internet!

As for all the prognostication on how it will sell, and will Apple save the publishing industry, will the iPad bring world peace or feed starving children, I have no clue. But I know I want one, and am planning on buying one.

Monday, January 25, 2010

Thoughts Regarding the Applet Tablet

If you have been following anything tech lately, you've heard rumors of an Apple tablet device. Rumors about this device have been going on for years now, but it seems that sometime real soon now this device will actually become a reality.

This Wednesday Apple is announcing something. Everyone thinks and hopes its an Apple tablet. Including yours truly. What happens if Apple does not announce it? There are going to be some sad, disappointed, and frustrated people out there... including me! Can we honestly blame Apple? No, they never said that is what they were going to announce. Though frustrating, it would be humorous if no tablet device was announced. All these people, getting themselves all jacked up for a new product that only exists in their minds! Hey I admit it exists in my mind too.

What's funny to me is how I am dreaming up what kind of apps I could write for it and I haven't even finished up an app for the iPhone yet!

All in all though, I really do think it highly likely that this device will be announced the day after tomorrow. I wish Wednesday would hurry up and get here!